4

javascript get the value of checkbox if checked without button clicked

I am struck in the middle of project, please help..

how to get the check box value from the class name or from by name or by id through alert so that the particular value I can pass...

I am sorry to ask..I am new the jquery and javascript..

This is my HTML ...

<input type="checkbox" name='cbox' value="red" class="theClass"/>red
<input type="checkbox" name='cbox' value="green" class="theClass"/>green
<input type="checkbox" name='cbox' value="yer" class="theClass"/>yer
<input type="checkbox" name='cbox' value="asdasd" class="theClass"/>asdasd
<input type="checkbox" name='cbox' value="radgdfged" class="theClass"/>radgdfged
$(function(){

$('input[type=checkbox]').on('change', function() {
   alert($(this).val());
    console.log($(this).val());
});

});

I googled much ...but every where onclick the button than only will get all checkbox values.. I am getting the values using the onclick button ....but the thing is getting the value without using the button... My concern is getting the value if checkbox is checked..

eg: if i checked red , i should get alert 'red'.....

2
  • explain fiddle example Commented May 12, 2015 at 7:45
  • you need to wrap the javascript code in <script type="text/javascript"> [CODE] </script> nodes Commented May 12, 2015 at 7:48

5 Answers 5

13

jQuery:

$('input[type=checkbox]').on('change', function() {
    console.log($(this).val());
});

Javascript:

var cbs = document.querySelectorAll('input[type=checkbox]');
for(var i = 0; i < cbs.length; i++) {
    cbs[i].addEventListener('change', function() {
        console.log(this.value);
    });
}

EDIT:

If you want the value only if the checkbox is checked.

jQuery:

$('input[type=checkbox]').on('change', function() {
    if($(this).is(':checked'))
        console.log($(this).val());
});

Javascript:

var cbs = document.querySelectorAll('input[type=checkbox]');
for(var i = 0; i < cbs.length; i++) {
    cbs[i].addEventListener('change', function() {
        if(this.checked)
            console.log(this.value);
    });
}

jsfiddle DEMO

P.s. for jQyery put your code inside:

$(function() {
    //code here
});
Sign up to request clarification or add additional context in comments.

8 Comments

thanks dude...but this condition is not working in the tree menu..I am getting the error in console ...please check ..Uncaught TypeError: $(...).on is not a function
That assumes you're using jQuery and put it inside $(document).ready, edited the answer.
If you're getting error you either not adding it in ready (see the last part in edit) or you're not loading jQuery. added a demo so you can check.
I have added like this :
$(function() { $(":checkbox").each(function() { alert($(this).val()) }); });
|
3

If you need to get all the checked checkboxes on change of checkboes, you can use:

$('.theClass').change(function(){
  alert($('.theClass:checked').map(function(){return this.value}).get())
});

Working Demo

2 Comments

Dude...please check..when I am unchecked also getting the alert box...please check
yes...thanks dude..please help me ..in this page also.....stackoverflow.com/questions/30099234/…
3

try:

<input type="checkbox" name='cbox' value="red" class="theClass" onchange="alert(this.value)"/>red

demo

EDIT:

<input type="checkbox" name='cbox' value="red" class="theClass" onchange="this.checked?alert(this.value):null"/>red

demo

EDIT

alert all checked input values:

$('input[type=checkbox]').on('change', function() {
if($(this).is(':checked')){
    var values = [];
    $.each($('input:checked'),function(index,input){
        values.push(input.value);
    });
    alert(values.join(','));
}    
});

demo

3 Comments

But only one error...please check ...when i unchecked also getting the value in alert box..please check
And one more ...query..if i checked ..multiple check values...all values are not coming in alert box...please check
@yangguange ...thanks dude...please help me in this page also : stackoverflow.com/questions/30099234/…
2

HTML

<input type="checkbox" name='cbox' value="red" class="theClass" checked/>

jQuery

$(function() {
    $(":checkbox").each(function() {
        alert($(this).val())
    });
});

1 Comment

Hi,, you code is when page is loaded...all check boxes values are getting alert without checking..
0

Javascript get the value of checkbox if checked without button clicked This code is working fine.Try this

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="utf-8">

<title>jQuery Get Values of Selected Checboxes</title>

<script type="text/javascript" src="http://code.jquery.com/jquery.js"></script>

<script type="text/javascript">

    $(document).ready(function() {

         $("input[type=checkbox]").change( function() {
                  var values = [];
                $.each($('input:checked'),function(index,input){
                    values.push(input.value);
                 });
             alert(values.join(','));

            var n = $( "input:checked" ).length;
            alert(n);
          });
    });

</script>

</head>

<body>

   <form id="booking_form">
        <input type="checkbox" name="bType" id="bType" value="A,200">&nbsp;Type A - USD200<br>
        <input type="checkbox" name="bType" id="bType" value="B,150">&nbsp;Type B - USD150<br>
        <input type="checkbox" name="bType" id="bType" value="C,100">&nbsp;Type C - USD100<br>
        <input type="checkbox" name="bType" id="bType" value="D,50">&nbsp;Type D - USD50<br>

    </form>

</body>

</html>

Preeti Dua

Comments

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.