shabarinath
shabarinath

Reputation: 593

How to pass multiple parameters to a onclick event binded via javascript variable

Iam trying to create a button with onclick event with 2 parameters that should be passed during loop. Iam able to pass variables and it is showing up on inspect element., but onclicking the button., it is giving an error 'unexpected end of input'. I passed multiple parameters as obj. Its returning [object object]. I tried stringify. But on button click., its giving unexpected end of input.

All I need is when clicking "buy" button., i need to retreive id, username of the table row. can u help me where I have done wrong. [code here][1]

Upvotes: 0

Views: 670

Answers (2)

jatin khattar
jatin khattar

Reputation: 46

I've just swapped " " with ' ' in the following line and it seems to be working fine.

var addBtn = item.status == "New" ? "<button id='buy-" + item.name + "' onClick='buyPet(" + JSON.stringify(buyTestobj) + ")' >Buy</button>" : item.status;

Hope this helps !

Upvotes: 1

Shahar G.
Shahar G.

Reputation: 1510

I wouldn't go this way but instead put a container with a button in the html, find the container/button and set it up with the id and onclick event (more of unobtrusive approach). Now regarding your question just pass an object containing whatever you need.

Upvotes: 0

Related Questions