2

In my php I have a for each loop that sets the order id. I wish to run a jquery function for each of the orders but I cannot get it to work.

HTML

<i id="<?php echo "edit".$row['order_id'] ?>" class="fa fa-pencil-square-o" aria-hidden="true"></i>

Jquery

$("<?php echo "#edit".$row['order_id'] ?>").click(function(){....}

The function runs but only for the second option. The question is how to pass the specific id variable to the script since the $row['order_id'] changes everytime and the script (I assume gets the final value)

5
  • Are both HTML and Jquery snippets contained in a .php script? Commented Aug 3, 2017 at 23:04
  • Yes they are at the same script. Actually the event gets triggered but only when I select the second button. It must be an issue with how I set my id's Commented Aug 3, 2017 at 23:06
  • @OluwafemiSule made some edits Commented Aug 3, 2017 at 23:10
  • 1
    How about assigning a common class to the icons and using the class name as a selector? Commented Aug 3, 2017 at 23:14
  • @OluwafemiSule I need to run the function for each specific item in the for each loop Commented Aug 3, 2017 at 23:19

1 Answer 1

3

Maybe what you need is a data property (placed via php) in your html, and then to read that property in a single event listener. For example:

<i data-orderid="<?php echo $row['order_id']; ?>" class="edits fa fa-pencil-square-o" aria-hidden="true"></i>

And then

$('.edits').click(function(event) {
    var rowId = $(event.target).data('orderid');
    // use the variable...
});
Sign up to request clarification or add additional context in comments.

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.