tags:

views:

11834

answers:

3

I tried to assign a new value into the hidden input and check box, it's worrking fine in firefox but not in IE (im using IE 7). Does any one know waht is going wrong with my code? Please see below for more informationL:

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;
+2  A: 

The code you pasted should work... There must be something else we are not seeing here.

Check this out. Working for me fine on IE7. When you submit you will see the variable passed in the URL.

Paolo Bergantino
A: 

Have a look at jQuery, a cross-browser library that will make your life a lot easier.

var msg = 'abc';
$('#msg').val(msg);
$('#sp_100').attr('checked', 'checked');
Stephan202
getElementById and value, checked are supported by every browser under the sun. jQuery is not the answer to everything in the world, and this problem hardly requires it.
Paolo Bergantino
@Paolo - True, but considering the elementary nature of the question, it's worth assuming that the asker is not aware of how much time he might save by using it, so I don't think this really deserves a reprimand.
karim79
@Paolo - true all browsers implement it... but IE (6/7) implements document.getElementById() wrong and jQuery avoids the bugs by heuristics for IE. http://webbugtrack.blogspot.com/2007/08/bug-152-getelementbyid-returns.html however in this case I don't believe this method is the issue.
scunliffe
+1  A: 

Jin Yong - IE has an issue with polluting the global scope with object references to any DOM elements with a "name" or "id" attribute set on the "initial" page load.

Thus you may have issues due to your variable name.

Try this and see if it works.

var someOtherName="abc";
//  ^^^^^^^^^^^^^
document.getElementById('msg').value = someOtherName;
document.getElementById('sp_100').checked = true;

There is a chance (in your original code) that IE attempts to set the value of the input to a reference to that actual element (ignores the error) but leaves you with no new value.

Keep in mind that in IE6/IE7 case doesn't matter for naming objects. IE believes that "foo" "Foo" and "FOO" are all the same object.

scunliffe