Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

document.getElementById().value and document.getElementById().checked not working for IE

Tags:

I tried to assign a new value into the hidden input and checkbox of an input form. It's working fine in Firefox but not in IE (I'm using IE 7). Does anyone know what is wrong with my code?

HTML:

<input type="hidden" id="msg" name="msg" value="" style="display:none"/> <input type="checkbox" name="sp" value="100" id="sp_100"> 

Javascript:

var Msg="abc"; document.getElementById('msg').value = Msg; document.getElementById('sp_100').checked = true; 
like image 923
Jin Yong Avatar asked Jun 09 '09 23:06

Jin Yong


People also ask

What is document getElementById () value?

HTML DOM Document getElementById() The getElementById() method returns an element with a specified value. The getElementById() method returns null if the element does not exist. The getElementById() method is one of the most common methods in the HTML DOM.

What can I use instead of document getElementById?

A commonly-used alternative to document. getElementById is using a jQuery selector which you read about more here.


1 Answers

For non-grouped elements, name and id should be same. In this case you gave name as 'sp' and id as 'sp_100'. Don't do that, do it like this:

HTML:

<input type="hidden" id="msg" name="msg" value="" style="display:none"/> <input type="checkbox" name="sp" value="100" id="sp"> 

Javascript:

var Msg="abc"; document.getElementById('msg').value = Msg; document.getElementById('sp').checked = true; 

For more details

please visit : http://www.impressivewebs.com/avoiding-problems-with-javascript-getelementbyid-method-in-internet-explorer-7/

like image 195
Pradeep Avatar answered Oct 01 '22 20:10

Pradeep