1

The json I receive looks like this:

Object:  
bpi:206000  
buyout:4120000  
itemId:133564  
itemName:"Spiced Rib Roast"  
name:"Flipflap"  
quantity:20  
timeLeft:"VERY_LONG"  
undercut:0

I'm populating a table through a jquery .each function.
One of the elements is set to fire of a function when it's pressed.
The code pushing items to the table looks like this:

$.each(data, function(i, key){
   items.push('<tr class="tr1"><td><a onclick="getItemData(' + key.itemId + ',' + key.itemName')" rel="item=' + key.itemId + '"">' + key.itemName + '</a>' + "</td><td>" + getAmount(key.buyout) + "</td><td>" + key.quantity + "</td><td>" + ifUndercut(key.undercut) + "</td><td>" + key.timeLeft.replace('_', ' ') + "</td></tr>");
});  

The problem is with this piece of the code:

onclick="getItemData(' + key.itemId + ',' + key.itemName')"  

This piece of code gives various variation of missing syntax errors.
Swapping out key.itemName with another key.itemId makes the onclick function work.
Is it the formatting of the string that's the problem?
How can I make it work with key.itemName?

1
  • The json I receive looks like this - there's the problem, that aint JSON Commented Nov 14, 2016 at 8:41

3 Answers 3

4

The issue is because itemName is a string, so you need to wrap it in quotes when it's output. You're also missing a + concatenating the string after the value. Try this:

items.push('<tr class="tr1"><td><a onclick="getItemData(' + key.itemId + ',\'' + key.itemName + '\')" rel="item=' + key.itemId + '"">' + key.itemName + '</a>' + "</td><td>" + getAmount(key.buyout) + "</td><td>" + key.quantity + "</td><td>" + ifUndercut(key.undercut) + "</td><td>" + key.timeLeft.replace('_', ' ') + "</td></tr>");

Note the addition of \' around the value.

I would also strongly suggest you research the use of both unobtrusive and delegated event handlers, as the on* event attributes are very outdated now and should be avoided where possible.

Sign up to request clarification or add additional context in comments.

3 Comments

So, why not $('<tr>').addClass('tr1') instead of '<tr class="tr1">'?
Thank you, that did it. I wasn't aware that I could use \. I tried every combination of '' and "" that I could think of.
@TânNguyễn you could do that too, although it would just make the example above extremely verbose. If you have that much HTML to add to the DOM I'd suggest you look in to a templating library
1

Your problem is the types of quotes you are using, a mix of double and single quotes

onclick="getItemData(" + key.itemId + ",\"" + key.itemName"\")"  

Comments

0

you missed the + operator: update this one:

      onclick="getItemData(' + key.itemId + ',' + key.itemName+')"  

4 Comments

How is your answer useful?
Firstly, the OP has given no indication that they are using angular at all. Secondly, even if they had your sample code is completely irrelevant to their problem
oops sorry i paste it on wrong side. let me change the answer. actully click event on dynamic html will be get by using delegate function.
You also need to add escaped string delimiters

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.