1

How to check checkbox checked or not using javascript ?

I have a lot of checkbox (dynamic data) . I want to know how can i check when checked chekcbox >= '1' using javascript ?

<input type="checkbox" id="1" name="test[]" onclick="test_fn()"/>
<input type="checkbox" id="2" name="test[]" onclick="test_fn()"/>
<input type="checkbox" id="3" name="test[]" onclick="test_fn()"/>
<input type="checkbox" id="4" name="test[]" onclick="test_fn()"/>

<script>
function test_fn() {
// check it's have checked checkbox or not ? //
}
</script>

2 Answers 2

2

Try to pass this in your inline handler,

<input type="checkbox" id="1" name="test[]" onclick="test_fn(this)"/>

And use this.checked to find out whether it is checked or not,

function test_fn(elem) {
 alert(elem.checked);
}

DEMO

To check, if the the checked check box greater than or equal to 1, then just do

function test_fn(elem) {
  if (document.querySelectorAll("input[name='test[]']:checked").length >= 1) {
    alert("yes it is greater than or equal to 1")
  }
}

DEMO

Sign up to request clarification or add additional context in comments.

9 Comments

@mongmongseesee Do you want something like that. See my answer.
no, i don't want to alert when checked, i want to check it's have checked checkbox >= '1' or not ?
on jsfiddle.net/9h4pek0L/3 when i uncheck for all it,s still alert yes it is greater than or equal to 1 ?
on jsfiddle.net/9h4pek0L/4 , When i checked 2 checkbox it's alert yes it is greater than or equal to 1 OK ^^ but when uncheck 1 checkbox why not alert yes it is greater than or equal to 1 ?
@mongmongseesee Sorry I misunderstood your requirement. Just check now, jsfiddle.net/9h4pek0L/5
|
1
function test_fn()
{
    if (this.checked) return true;
    else return false;
}

Edit:

this reference won't work in this case, thanks to @RajaprabhuAravindasamy for pointing this out, read the comments or check this SO question for more information.

You need to pass a reference explicitly to the checkbox element:

function test_fn (el)
{
    if (el.checked) return true;
    else return false;
}

As for how to know if checked boxes count is more than one you can simply use a global variable to keep a track of the count like this:

var count = 0;

function test_fn (el)
{
    el.checked ? count++ : count--;
    return count > 1;
    // if (count > 1) alert ("count is more than 1");
}

Demo

3 Comments

jsfiddle.net/9h4pek0L This will be output for this code. This inside a plain function will point the window object.
@RajaprabhuAravindasamy I was just testing it and this seems to be undefined, why is this not working?
this inside a plain function will point to window. This is not just like binding explicit event handler. So widow.checked will be evaluated to undefined.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.