Vignesh
Vignesh

Reputation: 559

document.getElementbyId is not working in IE 11

Am trying to fetch the entire div content and displaying in a alert message using document.getElementById. It is working fine in IE 8 & 9, but not in IE 10 & 11.

Below is the code snippet which am using.

<body>
  <div id="oDiv1"><input type="text" ></div>
  <div id="oDiv2">Div #2</div>
  <div id="oDiv3">Div #3</div>
  <input type="button" value="Show First DIV Content" onclick="fnGetId()">
</body>

Javascript code

<script type="text/javascript">
     function fnGetId() {
             var oVDiv=document.getElementById("oDiv1").innerHTML;
             alert(oVDiv);    
    }
</script>

Am entering some text in that textbox. In IE 8 & 9 its taking the oDiv1 content along with the text which I entered. Like below

<input type="text" value="aaa">

But in IE 10 & 11, its taking the input tag but not the value which I entered.

<input type="text" value="">

Where am I going wrong? How can I get the content along with the values which I entered in IE11?

Upvotes: 1

Views: 14583

Answers (1)

Ryan
Ryan

Reputation: 14649

Because you are not getting the right value and you are not getting the right element. Here is a hacky solution.

var oVDiv=document.getElementById("oDiv1").children[0].value;

A better pattern would be to create a real form, and serialize the form values on submit.

JSfiddle example

Upvotes: 4

Related Questions