funbinod Posted February 10, 2015 Share Posted February 10, 2015 (sorry if I had to ask it on php forum) inside a php while loop can I run jquery (or javascript) for each row??? I tried the following --------------- while ($row = ...................) { echo " <tr> <td>".$row['abc']."</td> <td><a id='delete' href='delete.php?m=$m&abc=$row[abc]'>DELETE</a></td> <script> $(document).ready(function(e) { $('#delete').click(function(e) { var conf = confirm('Sure Delete!?') if (!conf) { event.preventDefault(); } }); }); </script> </tr> ";} but this jquery script works for the first row only and not to other rows. how can I run this confirm script for each row generated by while loop????? thank u in advance.... Link to comment Share on other sites More sharing options...
justsomeguy Posted February 10, 2015 Share Posted February 10, 2015 You can output anything you want with PHP, including Javascript code. That Javascript code attaches a ready event to the document, which looks for an element with the ID delete and attaches a click handler to it. In HTML, IDs need to be unique, only one element can have any ID. That's why the code is only working on one element. Maybe use a class instead and just output a single piece of Javascript code at the end to attach the handler to all elements with that class. Link to comment Share on other sites More sharing options...
thescientist Posted February 10, 2015 Share Posted February 10, 2015 Agreed. Outputting elements / HTML in a loop is a good idea, but attaching event handles in a loop like that is a bad idea. Use the loop to generate your HTML, and then outside the loop output your JS snippet for attaching events. Link to comment Share on other sites More sharing options...
funbinod Posted February 10, 2015 Author Share Posted February 10, 2015 (edited) oh yes! thank u! I understood what u pointed. but I didn't what u suggested (coz I know less on using class). but I made one logic and solved it. I generated an unique id for each row and attached it to the 'id' and modified the jquery with respect to it. below is what I did----- $rn = 0;while ($row = ...................) { $rn++; echo " <tr> <td>".$row['abc']."</td> <td><a id='delete".$rn."' href='delete.php?m=$m&abc=$row[abc]'>DELETE</a></td> <script> $(document).ready(function(e) { $('#delete".$rn."').click(function(e) { var conf = confirm('Sure Delete!?') if (!conf) { event.preventDefault(); } }); }); </script> </tr> ";} what u think, this would be good to use or not? Edited February 10, 2015 by funbinod Link to comment Share on other sites More sharing options...
justsomeguy Posted February 10, 2015 Share Posted February 10, 2015 It's unnecessary when you could just have 1 piece of Javascript code for all elements, but if that's what you want to do then do it. Link to comment Share on other sites More sharing options...
funbinod Posted February 10, 2015 Author Share Posted February 10, 2015 what I want to do is just to make this work. I did this coz I know this. if I get chance to learn more efficient method, surely i'll change the code. will u guide me to there??? Link to comment Share on other sites More sharing options...
thescientist Posted February 10, 2015 Share Posted February 10, 2015 Just break it down for each recommendation Use a class instead of IDs Move the event handler outside of the loop $rn = 0;while ($row = ...................) { $rn++; echo " <tr> <td>" . $row['abc'] . "</td> <td><a class='delete' href='delete.php?m=$m&abc=$row[abc]'>DELETE</a></td> </tr> ";}echo " <script> $(document).ready(function(e) { $('.delete').click(function(e) { var conf = confirm('Sure Delete!?') if (!conf) { event.preventDefault(); } }); }); </script>"; 1 Link to comment Share on other sites More sharing options...
funbinod Posted February 11, 2015 Author Share Posted February 11, 2015 uhh uhh uhh uhh!!!!!!!! thanks for the suggestion. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now