Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
120
rated 0 times [  126] [ 6]  / answers: 1 / hits: 26017  / 11 Years ago, tue, august 6, 2013, 12:00:00

i often use this notation when i name my controls in order to get an array in POST or GET.



<input name=color[1] type=text />
<input name=color[2] type=text />
<input name=color[3] type=text />


so in my scripts i can do



<?php $data=$_GET[color]; 
for each ($color as $key=>$value) {
doSomething();
} ?>


Often happens that i need to get those id back in javascript , but i cannot get them , so i often add an ID to each element in html like that



<input name=color[3] id=color_3 type=text />


so that i can use document.getElementsById('color_3')



Instead i would like to find way to use document.getElementsByName(color[3])...
but i cannot really get it to work.



Any help?


More From » foreach

 Answers
20
<input name=color[3] id=color_3 type=text />

var element = document.getElementsByName(color[3]);

alert(element[0].id);


It works fine .. The thing you should have in your mind is Return type is an array of elements not a single element


[#76509] Sunday, August 4, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
chyanne

Total Points: 208
Total Questions: 120
Total Answers: 110

Location: Colombia
Member since Mon, May 2, 2022
2 Years ago
;