-3

Suppose we have

<form action="xxx">
    <input type="checkbox" id = "checkbox" name="checked" value="1">
</form>

Now want some jQuery or JS function to change the "value" attribute of this checkbox. In Chrome and Firefox, everything works fine but seems IE doesn't give much support on form. form elements' attributes are not able to be changed in IE. Any good idea about this? Thanks

dsgriffin
  • 66,495
  • 17
  • 137
  • 137
Edmond
  • 614
  • 2
  • 11
  • 26
  • 4
    What makes you think they can't be changed in IE? – dsgriffin Jun 07 '13 at 23:48
  • What IE version are you using? If you are using raw javascript, don't. Use jquery instead as it abstract browser differences in JavaScript interpretation for you. – Bojin Li Jun 07 '13 at 23:50
  • At least I didn't see IE7 or 8 work. – Edmond Jun 07 '13 at 23:52
  • Possible duplicate: http://stackoverflow.com/questions/426258/how-do-i-check-a-checkbox-with-jquery-or-javascript – Emil Lundberg Jun 08 '13 at 00:00
  • 1
    @EmilLundberg - That's not a duplicate. The other question is about setting the `checked` property. – nnnnnn Jun 08 '13 at 00:07
  • Yes. My difficulty is I cannot do this with IE9 or below – Edmond Jun 08 '13 at 00:08
  • 1
    If the two answers below don't work for you there is some other problem in your code that doesn't work in IE, nothing to do with setting the `value`. The only way we can help you with that is if you actually show your code in your question. Consider providing a demo at http://jsfiddle.net so that we can see the problem for ourselves. – nnnnnn Jun 08 '13 at 00:10
  • @ZenithI apologize for my misunderstanding. Only the "form" attribute of a form element is not supported by IE7 and IE8. Other attributes like "name", "value" etc should be fine. Thank you for pointing out. – Edmond Jun 08 '13 at 02:43

2 Answers2

2

Should just access it through the classic API

document.getElementById("checkbox").value = 2;
Travis J
  • 81,153
  • 41
  • 202
  • 273
0

"form elements' attributes are not able to be changed in IE"

I don't know where you got that idea. As far as I'm aware the only problem you might have with IE in this area is that it won't let you change the type attribute of inputs. Changing other attributes, including value, will work fine in IE as in other browsers. If you have a problem with IE it is likely related to some other aspect of your code - which we can't advise you about since you didn't include any code in your question.

Anyway, changing value without jQuery:

document.getElementById("checkbox").value = "new value here";

With jQuery

$("#checkbox").val("new value here");
nnnnnn
  • 147,572
  • 30
  • 200
  • 241