Getting document.getElementByName with a substring

matz

New Member
With the constraint that you can't use jquery, what's the best way to get all element names that start with a specific string? (These are checkboxes, and I want to clear all the others when one is checked. My solution works, but it seems less than optimal; is there a better approach? What I'm doing is putting the checkboxes within a div and getting all the children of the div, and then comparing their name to what I'm looking for. Here's the javascript: \[code\]<script language="javascript" type="text/javascript">function clearCheckboxes(name, id, divtag) { if (document.getElementById(id).checked == false) { return; } var listbox = document.getElementById(divtag); var list = listbox.childNodes; for(var i = 0; i < list.length; i++) { if (list.type != "checkbox") continue; if (list.id == id) continue; if (list.name.substr(0, name.length) == name) { list.checked = false; } }}\[/code\]and then the html looks like this: \[code\]<div id="tag_div_2"> <input type="checkbox" name="tags_Type_4" id="4" onclick="clearCheckboxes('tags_Type_',4,'tag_div_2');" />3 Stone Ring <input type="checkbox" name="tags_Type_3" id="3" onclick="clearCheckboxes('tags_Type_',3,'tag_div_2');" />Engagement Ring <input type="checkbox" name="tags_Type_2" id="2" onclick="clearCheckboxes('tags_Type_',2,'tag_div_2');" />Solitaire <input type="checkbox" name="tags_Type_5" id="5" onclick="clearCheckboxes('tags_Type_',5,'tag_div_2');" />Wedding Set</div>\[/code\]
 
Top