-7

Okay, I have some code like this:

<?php
for($i = 0; $i < 20; $i++) {
    $idx = $i;
?>
<input class="edit-btn" type="button" name="edit" value="" onClick="editMe(this)"/>
<?php } ?>

My problem is: in function editMe() (in javascript) how can I determine which button was clicked (according to $idx value)? Actually I don't like to set $idx as id of buttons and also I don't like to use $idx as their values.

Update: i -> $i, I want to emphasize that each button should associate to an unique index

duong_dajgja
  • 4,196
  • 1
  • 38
  • 65

1 Answers1

2

Pass the value of idx as second parameter to the function

Not sure about PHP syntax

<?php
for($i = 0; $i < 20; $i++) {
    $idx = $i;
?>
<input class="edit-btn" type="button" name="edit" value="" onClick="editMe(this, <?php echo $idx; ?>)"/>
<?php } ?>

then

function editMe(el, idx){
}
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • When the loop finishes execution, `$idx` will hold the value of the *last* iteration, which means, it will contain `19`. If the OP needs to create multiple buttons, then the `` block. – Amal Murali Feb 13 '14 at 12:44
  • @duong_dajgja it will be that... because we are using it only used to create the markup.... did you check the generated html and tested the functionality – Arun P Johny Feb 13 '14 at 12:58
  • @ArunPJohny: Okay, you're right. I tried your idea, it worked well. Thank you! I removed my comment that agreed with AmalMurali – duong_dajgja Feb 14 '14 at 04:08