streetparade
streetparade

Reputation: 32878

Check if Checkbox is checkd?

Im trying to get some checkbox with a specific name

document.getElementsByName("test");

Unfortunatley i cant check if it is checked or not here is the code

for(i=0;i<check.length;i++)
        {
            if(check[i].checked==true)
            {
                alert(check[i].value);
            }
        }

Is somewhere a typo?

Upvotes: 0

Views: 6220

Answers (3)

muruga
muruga

Reputation: 2122

Use the following code

<html>
<script type="text/javascript">
function test()
{

if(document.getElementById("chk").checked)
{

  alert('Checked');
}
}
</script>
<body>
<input type="checkbox" id="chk">
<input type="button" onclick="test();"></input>
</input>
</body>
</html>

Upvotes: 2

user142019
user142019

Reputation:

jQuery would be nice for these basic things.

But you aren't using jQuery so:

var check = document.getElementsByName("test");

instead of just

document.getElementsByName("test");

Also, you can remove ==true, so you get:

if(check[i].checked)

Which makes much cleaner code.

Also, are you sure you set the name of the checkboxes to "test" (sometimes people forget these things, like me every time ^^)

jQuery Example

First, download jQuery from http://jquery.com/

$("input[type=checkbox][name=test]:checked").each(function() {
    alert($(this).val());
});

That should do it. If you aren't familar with jQuery, look at this: http://docs.jquery.com/Tutorials:How_jQuery_Works

Upvotes: 3

RubyDubee
RubyDubee

Reputation: 2446

for(i=0;i<check.length;i++)
        {
            if(check[i].checked==1)
            {
                alert(check[i].value);
            }
        }

Try this ?

@streetparade : tell me if this is also not working .... so that i can delete my answer... people didn't like it

Upvotes: -1

Related Questions