最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - document.getElementById is null - Stack Overflow

programmeradmin3浏览0评论

I have the following JavaScript working in IE, but not on Chrome and Firefox. Here is the code:

<script type="text/javascript">
jQuery(document).ready(function() {

jQuery('#applicationSelect').change(function() {
document.getElementById('dropdown').value = "APPLICATION";
});

});
</script>

<input type="hidden" name="dropdown" value="" />

When I look in Firebug, the code errs:

document.getElementById("dropdown") is null
document.getElementById('dropdown').value = "APPLICATION";

I need your advice. Thank you in advance.

I have the following JavaScript working in IE, but not on Chrome and Firefox. Here is the code:

<script type="text/javascript">
jQuery(document).ready(function() {

jQuery('#applicationSelect').change(function() {
document.getElementById('dropdown').value = "APPLICATION";
});

});
</script>

<input type="hidden" name="dropdown" value="" />

When I look in Firebug, the code errs:

document.getElementById("dropdown") is null
document.getElementById('dropdown').value = "APPLICATION";

I need your advice. Thank you in advance.

Share Improve this question edited Feb 16, 2012 at 16:15 user1385191 asked Feb 16, 2012 at 7:45 JemruJemru 2,11918 gold badges39 silver badges53 bronze badges 7
  • I can't see element with 'dropdown' id. – Hadas Commented Feb 16, 2012 at 7:48
  • Are you sure you have element with id "dropdown" in your html? – Chuck Norris Commented Feb 16, 2012 at 7:48
  • where have you declared a tag with ID dropdown?? – Sunil Kumar B M Commented Feb 16, 2012 at 7:48
  • Include your HTML for #applicationSelect and #dropdown. – Stefan Commented Feb 16, 2012 at 7:48
  • Hi All, yes it way my typo error, it should be "dropdown" not "submitAs". Thanks. – Jemru Commented Feb 16, 2012 at 7:52
 |  Show 2 more ments

2 Answers 2

Reset to default 5

If you're using jQuery already, why not use it to set your input value:

$('#dropdown').val('APPLICATION');

And as others noted, your input's id was not 'dropdown'.

Also, note that to one may use $ in place of jQuery in most use-cases.

The problem is that your input element doesn't have an id! Change name to id, or add an id too:

<input type="hidden" name="dropdown" id="dropdown" value="" />

Alternatively, if you don't want to add an id, you could use getElementsByName (which doesn't work very well cross-browser), or you could use a jQuery attribute selector:

$("input[name='dropdown']").val("APPLICATION");

Update

I just noticed that you said in your question that it was working in IE. That means you must be using a pretty old version of IE, because getElementById in IE7 and below incorrectly selects elements by name, as well as id.

发布评论

评论列表(0)

  1. 暂无评论