Jump to content

Dynamic div id value? Can i get the innerhtml?


somedude

Recommended Posts

If i have a loop like this

 

<%
For i= 1 to 5
%>
<div id="txt<% response.write(i) %>">Hello<% response.write(i) %></div>
<%
next
%>

Is it possible to get the value of the div with id txt3?

I'm thinking something like this but i don't know the syntax

­<script>
var one=document.getElementById("txt*.* lol ???")­ .innerHTML
alert(one)
</script>

 

And no i can't just use getElementById("txt3") because i won't always be needing txt3. This is related to some AJAX work i'm doing. In the

 

function stateChanged() 
{ 
if (xmlHttp.readyState==4)
{ 
document.getElementById("sendvalue").innerHTML=xmlHttp.responseText;
}
}

The value sendvalue is dynamic. The value may be sendvalue1 or sendvalue45. Thanks for any help.

 

You can access elements by using variables. So for instance if you know that you have 5 elements and the elements are name txt1, txt2, etc. then you can access all of the HTML values of those elements with the following code:

 

var i;
var elementHTML;
for(i = 1; i <= 5; i++){
elementHTML = document.getElementById('txt' + i).innerHTML;
// do something with your elementHTML
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.