views:

162

answers:

3

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?

A: 
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

Webbisshh
-1. The `checked` *is* a boolean. Comparing to 1 is even worse than comparing to `true`.
KennyTM
Just for your information @KennyTM ... http://www.devx.com/tips/Tip/13204 ... thanks for downvote ;)
Webbisshh
@Webb: Find a more reliable source. http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-30233917 http://msdn.microsoft.com/en-us/library/ms533556%28VS.85%29.aspx https://developer.mozilla.org/en/DOM/Input
KennyTM
@Webbisshh: `1 == true`, which makes your solution no different to the OP's whatsoever.
Andy E
Both works for me... So i was just giving another solution.
Webbisshh
+3  A: 

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

Time Machine
But that was not working in his case?
Webbisshh
Info: i also assignet the var check to document.getElementsByName("test");
streetparade
Can you post a jquery example?
streetparade
There you go ;D
Time Machine
I've gotta say, the standard javascript on this one reads easier than the JQuery for me.
Dean J
@Dean really? Me hatez standard JavaScript hehe. I always use jQuery, because I like the CSS selector way to select elements.
Time Machine
@Koning Baard: jQuery is written in "standard JavaScript". Also, DOM selectors are already available in modern browsers (including IE8) with `querySelector()` and `querySelectorAll()`.
Andy E
@Koning Baard XIV: if(document.getElementById("chk").checked) {} requires less thinking than $("input[type=checkbox][name=test]:checked").each(function() { });
Dean J
+2  A: 

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>
muruga