2

In my page i am iterating values based on the database table. I have to give a alert message when i check my radio button residing in the php while loop, when users click on it.

I tried the below code, but its not working. When i am clicking on any of the radio button from my page then , i am not getting alert message . Please help me to solve my problem.

my php code:

      <?php  $result = mysql_query("SELECT * FROM group_expenditure_details where creater_id =   
       '$uid'");
           $a=0; 
           while($row = mysql_fetch_array($result))
               {

                       echo "<tr>";
                           echo "<td width='20%' align='center'><input type='checkbox'  
       name='test".$a++."'  id='test".$a++."' value='1'  /></td>";
                               echo "</tr>";
                }
              echo "</table>";
              echo "</div>";
              mysql_close($con);
           ?>

My Jquery code:

    <script type="text/javascript">
            $(function(){
             <?php  $od=$_SESSION['id']; $result1 = mysql_query("SELECT * FROM            
                   group_expenditure_details where creater_id = '$od'");  $a=0;
             while($row = mysql_fetch_array($result1))  {  ?>
                $('<?php  '#test'.$a++?>').click(function(){
            <?php } ?>
                    alert('clicked');
                });
            }); 
    </script>
1
  • you don't generate click handlers from loop. That's very bad.Follow Dipesh Parmarrs solution and replace your js entirely. Commented Apr 1, 2013 at 4:39

3 Answers 3

4
$(function()
{
    $('input[id^="test"]').on('click',function()
    {
        alert('Clicked');
    });
});

what $('input[id^="test"]') this will do is select all the input which id is starts with test.

.on is replacement of .live() and .delegate() in new version of jQuery.

.on() works on dyanmically loaded content so if checkbox added later then click event will also work on newly added DOM.

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

10 Comments

but i said already, i have a single checkbox in a loop, which creates multiple id. when click on any specific checkbox then alert message should be poppup
And what things i have to change in my php script i have shown above my php scripts?
@BurlsWillis no need to change php code just use above answered jQuery code
well its working fine with your above j query function. i kept <input type='checkbox' id='test' value='12'/> . Now instead of alert message ,i want this checkbox value to be shown in alert box. How to do this?
@BurlsWillis just replace alert() code with alert($(this).val());
|
0

I'll add a class attribute to the radio buttons

<input type='checkbox' class="my-radio" name='test".$a."'  id='test".$a."' value='1'  />

Then

$(function(){
    $('.my-radio').click(function(){
        alert('Clicked');
    });
});

Comments

-1
$(function(){  //ensure dom is ready
    $('input[id^=test]').click(function(){ //id starts with test
        alert('Clicked');
    });
});

Don't use any PHP in your javascript for this. The way you construct your id='test'.$a will ensure that the id `always starts with test. So within that click function above, we have handled the pattern.

1 Comment

but i said already, i have a single checkbox in a loop, which creates multiple id. when click on any specific checkbox then alert message should be poppup

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.