Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Elements and id's

Status
Not open for further replies.

guyzdancin1

Programmer
Jun 10, 2008
1
US
I have a jsp page of check boxes, some of which have identical id's. When I click a checkbox (selected or unselected), I want all the rest of the check boxes of the same id to switch to the same state as the box I just clicked. My problem is that the check boxes are dynamically loaded to the jsp, ultimately from a properties file. So I cannot pass a literal check box id to a java script function in the "onclick" attribute of the input tag. The java script method needs to know within the method block the check box id value and the state of the check box.

Here is the code I have with pseudo code where my knowledge is lacking.

You help is thanked in advance

======================================== CODE ==========================================

function checkBoxSynch(formName){
var allCheckBoxes = document.getElementById(formName);

var currentCheckBox = ** GET THE CHECKBOX THAT WAS JUST CLICKED **
var state = currentCheckBox.checked;

for(var i = 0; i < allCheckBoxes.elements.length; i++ ){

if(currentCheckBox.name == allCheckBoxes.elements.name )
allCheckBoxes.elements.checked = state;
}
}
 
I have a jsp page of check boxes, [!]some of which have identical id's.[/!]

There's your problem. IDs must be unique over the whole page. Given also that "getElementById" only returns a single element, this should makes sense.

You are better off using either the NAME or possibly even the CLASS attribute to iterate over your inputs.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top