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.

 

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.