is there any way to get a id that was created dynamically into a JavaScript function i know its possible to get a specified id using document.getElementById is there a function similar or a similar method for getting id on the fly any help would be greatly appreciated thanks
well the code goes like this the HTML these are created from items in a database <img id="label1" src="http://www.example.com" /> <img id="label2" src="http://www.example.com" /> <img id="label3" src="http://www.example.com" /> i have no problem passing the id from the images onclick to the java code, the problem is getting it to be dynamic. document.getElementById would only work for the id you specified like below the java document.getElementById(label1) //because from what i understand you must specify an id when using document.getElementById... therefore it can never be dynamic it would always be the same. this code would only get the first id (label1) out of the three i also tried this document.getElementById(label1) document.getElementById(label2) document.getElementById(label3) this didnt make any difference, instead all it did now was get the id of the last image (label3). and never paid any attention to the first two calls
What are you trying to do after you get the id? You need to store the values using an array [COLOR=#000000][FONT=courier new]var images=new Array(); [/FONT][/COLOR] [COLOR=#000000][FONT=courier new]for(i=0; i<3; i++){ var increment = i+1; images[i]=[/FONT][/COLOR][COLOR=#111111]document.getElementById('label'+increment);[/COLOR][COLOR=#000000][FONT=courier new] }[/FONT][/COLOR] Code (markup): images[0] will be the first <img> images[1] will be the second <img> images[2] will be the third <img> .... and so on
so here is the original code inside the java file maybe yo understand it bettere than i do var button = document.getElementById('button'); switch(button.name) { case 'button0': button.src = 'images/1.jpg'; button.name = 'button1'; break; case 'button1': button.src = 'images/0.jpg'; button.name = 'button0'; break; } can i use your code to do the same thing