admin管理员组

文章数量:1401171

I am trying to set dynamically innerHTML for value tag in IE.

The output I expect is this.

<input type="hidden" >a0FQ0000009rJfCMAU</input>

I tried innerHTML in IE but it was not working since it is supported for INPUT element. So, I tried the following out of desperation.

myElementId.setAttribute("value",textNodeId);

Unfortunately this is not what I want.

<input type="hidden" value="a0FQ0000009rJfCMAU">

Please advise.

I am trying to set dynamically innerHTML for value tag in IE.

The output I expect is this.

<input type="hidden" >a0FQ0000009rJfCMAU</input>

I tried innerHTML in IE but it was not working since it is supported for INPUT element. So, I tried the following out of desperation.

myElementId.setAttribute("value",textNodeId);

Unfortunately this is not what I want.

<input type="hidden" value="a0FQ0000009rJfCMAU">

Please advise.

Share Improve this question asked Nov 21, 2011 at 12:26 bragboybragboy 35.6k30 gold badges116 silver badges175 bronze badges 4
  • 1 Do <input> elements even support CDATA? I think not. – Linus Kleen Commented Nov 21, 2011 at 12:29
  • 2 If the input is hidden, why does it need to be innerHTML and not value? – user479870 Commented Nov 21, 2011 at 12:30
  • @pdknsk : Good question which i don't have an answer.. the piece of code i've given is a sample of an existing framework/library. – bragboy Commented Nov 21, 2011 at 12:32
  • 1 Why are you wanting to do this? The hidden type of input element does not support innerHTML, nor should it. a hidden element should always have it's value in the value field which you can access by using myElementId.value(textNodeId); – kamui Commented Nov 21, 2011 at 12:33
Add a ment  | 

2 Answers 2

Reset to default 3

If you check the input element's specification, you see that its Content Model should be empty. Thus, what you try to find is logically invalid HTML.

See here.

If you want to change or set the value of input tag dynamically, you have to first define its name or id and then in javascript you can set it like

var txt = document.getElementById('yourtext');
txt.value = 'new value';

I hope this helps you.

本文标签: javascriptHow to set a inner value for Input tag in HTML in IEStack Overflow