user1889966
user1889966

Reputation: 165

Unable to pass parameter to onclick function inside button tag

I have the following line in my code:

items.push("</font><button onclick='addToItinerary();' class='coolbutton' style='float: right;' id='" + results[i] + "'>add</button></ol><br> </br>");

I want to pass a parameter into my addToItinerary function (results[i]), but despite reading several responses to similar questions, I still can't seem to figure it out! What's going on? Thank you in advance.

Upvotes: 0

Views: 853

Answers (1)

Barmar
Barmar

Reputation: 780688

Pass this as the parameter. The function will then receive the button element as the argument. To get results[i] it can access the .id attribute of the argument.

items.push("</font><button onclick='addToItinerary(this);' class='coolbutton' style='float: right;' id='" + results[i] + "'>add</button></ol><br> </br>");

Upvotes: 1

Related Questions