Sivadinesh
Sivadinesh

Reputation: 157

getElementBy ID not returning for label tag

getElementById does not return any value. My objective is I have a text content and upon a action I have to replace the value of the text. I use labels to display.
Is there any better way to display such modified text, I dont want to use textbox.

<% for(int i=0; i<lines.length;i++) {
  if(lines[i].contains(" ")) { %>
   <label id='idkey<%=i%>' name='key1<%=i%>'>ABC</label>
                          <%
  }
} %>


for(j=0; j<len; j++){
var lblElement = document.getElementById('idkey'+j).innerText="ddd";
  alert(lblElement);
}

Upvotes: 0

Views: 387

Answers (3)

Daab
Daab

Reputation: 101

The line var lblElement = document.getElementById('idkey'+j).innerText="ddd"; does not set lblElement equal to the element - but rather equal to the text "ddd". Try the following in stead:

for(j=0; j<len; j++){
  var lblElement = document.getElementById('idkey'+j);
  lblElement.innerText="ddd";
  alert(lblElement);
}

Note: This assumes that the len variable contains the correct value

Upvotes: 0

kevpoccs
kevpoccs

Reputation: 635

Re hi,

I think your Id is wrong, innerText work properly with http://jsfiddle.net/mPc2E/ Be carreful, innerText return the value of you new text, not the modified tag.

document.getElementById('lab').innerText = " toto"

Upvotes: 1

Nouphal.M
Nouphal.M

Reputation: 6344

Try

document.getElementById('idkey'+j).innerHTML = 'ddd';

Demo here

Upvotes: 0

Related Questions