1
    <?php
    while($query=mysql_fetch_assoc($select)){
        ?>
        <tr><td><input type="checkbox" name="checkBoxMail" id="checkBoxMail" 
        value="<?php echo $query['id']; ?>" 
        userid="<?php echo $query['suserid']; ?>"></td>

        </tr>
        <?php
    }
    ?>

This code created multiple checkbox in view page. First check only return value after that next check box click is not working

$('#checkBoxMail').click(function(){

    alert("alert");
});
2
  • You have to use delegate event binding. $('#checkBoxMail').click not works on dynamically created html. Have to do $(document).on('click','.checkBoxMail', function(){ }); Commented Jan 12, 2016 at 11:09
  • you are using checkBoxMail as ID thats why for multiple same ID is not valid. For this you have to change ID to class='checkBoxMail' and remove ID from input tag $('.checkBoxMail').click(function(){ alert("alert"); }); Commented Jan 12, 2016 at 11:28

6 Answers 6

6

There would be multiple checkboxes with same id which is wrong. Try with class.

<input type="checkbox" name="checkBoxMail" class="checkBoxMail" 
value="<?php echo $query['id']; ?>" 
userid="<?php echo $query['suserid']; ?>">

And

$('.checkBoxMail').click(function(){
    alert("alert");
});

And if you want to access the value or attribute then simple do -

$(this).val();
$(this).attr('userid');
Sign up to request clarification or add additional context in comments.

2 Comments

you need to handle delegate cz checkbox geneated by loop depend on data
i highly doubt that your .click will work in this case.
3

Three examples of how you can do this by ID:

Remember if you're creating multiple checkbox fields don't set the same ID, make a different ID for each one or you can select the checkbox by class by changing the # to . as you can use the classname multiple times.

The ID has to be unique

$(document).ready(function()
{
  // By ID - ID has to be unique
  $('#checkBoxMail').on("click", function()
  {
    alert("alert");
  });
  
  $('#checkBoxMail2').click(function()
  {
    alert("alert2");
  });
  
  $(document).on("click", "#checkBoxMail3", function()
  {
    alert("alert3");
  });
  
  // By classname
  $('.checkBoxMail5').on("click", function()
  {
    alert("alert");
  });
  
  $('.checkBoxMail5').click(function()
  {
    alert("alert2");
  });
  
  $(document).on("click", ".checkBoxMail5", function()
  {
    alert("alert3");
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Divs with unique ID
<hr>
<div id="checkBoxMail">CLICK ME</div>
<div id="checkBoxMail2">CLICK ME</div>
<div id="checkBoxMail3">CLICK ME</div>


<br>
Div with classname
<hr>
<div class="checkBoxMail5">CLICK ME</div>

3 Comments

id must be unqiue . you can't work with same id mutiple time . that is dynamic dom so it'll need delegate your first two way won't work .
But op need click on checkbox not in entire table
@AnikIslamAbhi - I've changed my answer to say about ID's
0

You are using same id for all checkbox creted, either use unique id for each checkbox or use class. See below code, where i have used class

<?php
    while($query=mysql_fetch_assoc($select)){
        ?>
        <tr><td><input type="checkbox" name="checkBoxMail" class="checkBoxMail" 
        value="<?php echo $query['id']; ?>" 
        userid="<?php echo $query['suserid']; ?>"></td>

        </tr>
        <?php
    }
    ?>

jQuery:

$(document).on('click','.checkBoxMail', function(){
   alert("alert");
});

Comments

0

Give class to all and Read about delegated event handlers , that would help: http://api.jquery.com/on/

$(document).on("click",".classname",function(event){
     alert("hi");
 }); 

Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. By picking an element that is guaranteed to be present at the time the delegated event handler is attached, you can use delegated events to avoid the need to frequently attach and remove event handlers

Comments

0
 $(document).ready(function () {

    $('#checkBoxMail').click(function () {

    alert("click event fired")});

});

<input type="checkbox" name="checkBoxMail" id="checkBoxMail" />

Comments

0
$('#checkBoxMail').is(':checked'){
    alert("alert");
});

Try this. Enjoy!

3 Comments

That's incomplete (it's only part of a conditional) and invalid javascript.
or he can use document.getElementById('checkBoxMail').checked It will work too.
That's half a conditional and half a callback.

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.