1

javascript is only taking first checkbox value it is not taking other checkbox value why ? HTML:

<input type="checkbox" id="checkbox" onclick="check()" value="<?php echo $data->id; ?>  " >

Javascript:

    function check()
     {    
          var id = $('#checkbox').attr('value')

         var checkbox = document.getElementById('checkbox');
        if(checkbox.checked==true)  {
            alert(id);
        }
        else
        {
            alert('false');
        }

     }
3
  • <input type="checkbox" id="checkbox" onclick="check()" value="<?php echo $data->id; ?> " > Commented Apr 22, 2019 at 2:00
  • please add your html in your question Commented Apr 22, 2019 at 2:08
  • <input type="checkbox" id="checkbox" onclick="check()" value="<?php echo $data->id; ?>" > Commented Apr 22, 2019 at 2:24

2 Answers 2

2

you can try like this with jQuery. here in console you can see all checked value is displaying after click

$(document).ready(function() {
  var ckbox = $("input[name='checkbox']");
  var chkId = '';
  $('input').on('click', function() {
    
    if (ckbox.is(':checked')) {
      $("input[name='checkbox']:checked").each ( function() {
   			chkId = $(this).val() + ",";
        chkId = chkId.slice(0, -1);
 	  });
       
       console.log( $(this).val() ); // return all values of checkboxes checked
       //console.log(chkId); // return value of checkbox checked
    }     
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="checkbox" name="checkbox" value="12520">
<input type="checkbox" name="checkbox" value="12521">
<input type="checkbox" name="checkbox" value="12522">

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

1 Comment

Its working thanks @Lemon Kazi you save my time.......?????thanx again
0

Try this instead.

$("'#checkbox").live("click", function () {

 $("input:checkbox[name=chk]:checked").each(function () {

  alert("Id: " + $(this).attr("id") + " Value: " + $(this).val());

   });

  });

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.