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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping Through Checkbox and check by name match

Status
Not open for further replies.

jtrembla

ISP
Jul 6, 2006
104
US
I am fairly new to JS and was wondering how would I loop through checkboxes on a form and only check those where name is like _*_2_*_

For example, this checkbox name would be checked, _274_2_410_ but this one would not _274_3_410_

<html>
<head>
<title>Untitled Document</title>
</head>
<script>

function checkall()
{
for(i=0; i<document.Form1.elements.length; i++)
{
if(document.Form1.elements.type=="checkbox")
{
document.Form1.elements.checked=true;
}
}
}


</script>
<body>

<a href="#" onclick="checkall()">Check All</a>
<form action="" method="get" name="Form1">
<input id="_274_2_410_" type="checkbox" name="_274_2_410_" />
<input id="_275_2_410_" type="checkbox" name="_275_2_410_" />
<input id="_274_3_410_" type="checkbox" name="_274_3_410_" />
</form>
</body>
</html>
 
something like this...

Code:
function checkall()
{
    var e = document.forms['Form1'].elements;
    for ( var i = 0; i < e.length; i++ ) {
        if (e[i].type == 'checkbox') {
            if (e[i].name.indexOf('_2_') > -1) {
                e[i].checked = true;
            }
        }
    }
}



*cLFlaVA
----------------------------
[tt]mr. pibb + red vines = crazy delicious![/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top