meomike2000 Posted July 8, 2009 Share Posted July 8, 2009 i need a way to create a loop that will create a variable that i can increment by one each time..... something like this for (var i = 1; i < source.length; i++) { var increment_this = document.getElementById('this_element' + i); } what i am trying to accomplish is that i have a function that uses dom to create <div> to hold some info from database.... this is random depending on the user so i use a for() loop to increment the id of the new <div>.... i want to be able to double click on the div itself to toggle that to another function that will change what is inside that div from what is being shown........ kind of like an edit in place....... i only need to know how to do the above incrementation....... thanks mike. Quote Link to comment Share on other sites More sharing options...
corbin Posted July 8, 2009 Share Posted July 8, 2009 You mean you want to create divs and assign them an ID? https://developer.mozilla.org/en/document.createElement https://developer.mozilla.org/en/DOM/element.id https://developer.mozilla.org/en/DOM/element.appendChild Quote Link to comment Share on other sites More sharing options...
meomike2000 Posted July 9, 2009 Author Share Posted July 9, 2009 no i can create the div and and assign them an id just fine..... i want to be able to select the div with a dblclick and each user will have a diff amount of divs on the page,,,, some have more data entries than others....... i create the div with a for() loop using a counter like so counter = 0; for() { document.createElement('div' + counter); counter++; } now i need a way to document.getElementById('div' + counter); i need to increment_this = document.getElementById('div' + counter); hope this help to understand what i need..... Quote Link to comment Share on other sites More sharing options...
haku Posted July 9, 2009 Share Posted July 9, 2009 Not 100% sure what you mean, but one way you can maybe do what I think you might be trying to say, is to create a global counter, then increment the value in a function. So your javascript document would look something like this: var counter = 0; function increment_counter() { counter += 1; alert(counter); } window.onload = function() { var target = document.getElementById("target") target.click = function() { increment_counter() } } By creating 'counter' globally outside of any functions, it holds its value even outside the function. Not sure if that is exactly what you are looking for though. Quote Link to comment Share on other sites More sharing options...
meomike2000 Posted July 9, 2009 Author Share Posted July 9, 2009 ok may be i can try to explain better.... different users have different amount of options stored in database.... d has 3 options... j has 5.... h has 10... and so on.. so i use a for loop to create divs and use a counter to increment the divs, like so div = document.createElement('div') div.id = 'thisdiv' + counter giving me div0, div1, div2 and so on..... now i need a way to do something like increment + counter = document.getElementById('thisdiv' + counter) increment + counter.ondblclick = do something for each of the div s, so i would think that a for loop would work, the problem is that when i double click on any of the options i only get the value of the last div......... so i need a way to do the above so that the var named increment is incremented inside the for loop..... hope that this makes more sense.... thanks mike..... Quote Link to comment Share on other sites More sharing options...
corbin Posted July 9, 2009 Share Posted July 9, 2009 So.... Wouldn't an array be perfect for this? var divs = new Array(); for(var i = 0; i < 10; ++i) { //create a div named divi (example, div0, div1, ...) divs = (reference to the div); //example: divs = document.getElementById('div' + i); } Quote Link to comment Share on other sites More sharing options...
meomike2000 Posted July 10, 2009 Author Share Posted July 10, 2009 well i think that would work but i figured out that i didnt need to do that at all..... just putting div.ondblclick = self.toggle; fixed the problem for me....... i was trying to make it to complicated i guess...... i was under the impression that i would need a value for each on when really i didnt..... thanks for all that responded.... mike here is the final code that i am using, converted to work with my ajax.... insertfriend = function(name,pic,status,logtime,logdate,newid) { var self = getfriends; var div = self.Div; var entryFirst = null; var fddiv = document.createElement('div'); fddiv.id = 'friend' + newid; fddiv.className = 'friend'; var fnamediv = null; var fpicdiv = null; var fpic = null; var fonlinediv = null; var flogtime = null; var flogdate = null; fnamediv = document.createElement('div'); fnamediv.id = 'fnamediv' + newid; fpicdiv = document.createElement('div'); fpic = document.createElement('img'); fpic.src = pic; fpicdiv.id = 'fpic' + newid; fpic.className = 'fpic'; fonlinediv = document.createElement('div'); fonlinediv.id = 'fonlinediv' + newid; flogtimediv = document.createElement('div'); flogtimediv.id = 'flogtimediv' + newid; flogdatediv = document.createElement('div'); flogdatediv.id = 'flogdatediv' + newid; fnamediv.appendChild(document.createTextNode(name)); fnamediv.appendChild(document.createTextNode(newid)); fpicdiv.appendChild(fpic); fddiv.appendChild(fnamediv); fddiv.appendChild(fpicdiv); fonlinediv.appendChild(document.createTextNode(status)); flogtimediv.appendChild(document.createTextNode(logtime)); flogdatediv.appendChild(document.createTextNode(logdate)); fddiv.appendChild(fonlinediv); if (status == "online") { fonlinediv.className = 'fonline'; } else { fddiv.appendChild(flogtimediv); fddiv.appendChild(flogdatediv); } div.appendChild(fddiv); div.ondblclick = self.toggle; }; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.