javascript - document.getElementById does not work on Firefox - Stack Overflow

Firefox returns null when I try to call document.getElementById on an element.Here is the call document

Firefox returns null when I try to call document.getElementById on an element.

Here is the call

document.getElementById('interaction4793')

However the element is present in the DOM. I can find it

<interaction id="interaction4793">
    <action id="action3268" trigger="enter" type="hover" />
    <reaction delay="0" id="reaction3709" options="reloadOnly" target="page0001" transition="none" type="showPage" />
</interaction>

Even when I select it from the inspector and use the $0 trick it returns null:

document.getElementById($0.id)

When I try to get the elements from getElementsById it works.

EDIT: Chrome and Safari do not return null. I tested Firefox (version 26) on Window and MacOS both return null.

EDIT2: I think Firefox do not understand that the attribute id is the id. When I search by attribute with jquery I can find the interaction:

console.log($('#interaction102').length); // returns 0
console.log($('[id="interaction102"]').length); // returns 1

Firefox returns null when I try to call document.getElementById on an element.

Here is the call

document.getElementById('interaction4793')

However the element is present in the DOM. I can find it

<interaction id="interaction4793">
    <action id="action3268" trigger="enter" type="hover" />
    <reaction delay="0" id="reaction3709" options="reloadOnly" target="page0001" transition="none" type="showPage" />
</interaction>

Even when I select it from the inspector and use the $0 trick it returns null:

document.getElementById($0.id)

When I try to get the elements from getElementsById it works.

EDIT: Chrome and Safari do not return null. I tested Firefox (version 26) on Window and MacOS both return null.

EDIT2: I think Firefox do not understand that the attribute id is the id. When I search by attribute with jquery I can find the interaction:

console.log($('#interaction102').length); // returns 0
console.log($('[id="interaction102"]').length); // returns 1
Share Improve this question edited Jan 17, 2014 at 16:24 Charles asked Jan 17, 2014 at 13:28 CharlesCharles 11.8k10 gold badges81 silver badges122 bronze badges 6
  • 5 When do you call document.getElementById('interaction4793')? – CodingIntrigue Commented Jan 17, 2014 at 13:30
  • 2 Works for me with Firefox – Andy Commented Jan 17, 2014 at 13:30
  • Yes it's strange, it works for me too on a blank page. When I try it in my webapp, it returns always null... – Charles Commented Jan 17, 2014 at 13:32
  • I call it during the use of the webapp. The DOM is already ready and the element is there. – Charles Commented Jan 17, 2014 at 13:33
  • 1 Are you sure you don't have another tag with the same id? – Elliott Frisch Commented Jan 17, 2014 at 13:51
 |  Show 1 more ment

1 Answer 1

Reset to default 1

The question is already answered here: JavaScript getElementByID() not working

Sometimes happen when the content of your website is not already loaded and crash.

Hope to be helpful

EDIT1: You can also try this: var yourvar= $("#interaction4793")[0]; It works for me

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745312400a4622059.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信