3

I'm trying to pass an object to a function in the onclick event. If I run the code below, I get Uncaught SyntaxError: Unexpected indentifier. If I uncomment the JSON.stringify line, and use taskAsJson in place of task, I get the same error.

$.each(tasks, function (index, task) {
    //var taskAsJson = JSON.stringify(task);
    items.push('<li><a href="#" onclick="showTask(' + task + ');">' + task.Description + '</a></li>');
});

However, if I hard-code a value, like 5, it works and 5 gets passed to the function.

$.each(tasks, function (index, task) {
    items.push('<li><a href="#" onclick="showTask(' + 5 + ');">' + task.Description + '</a></li>');
});

Why do I get that error when I try to pass my object? What do I need to change in order to be able to pass task or taskAsJson?

Note: items is an array that gets used to update a div and listview:

$('#appTasks').append(items).listview('refresh');
1
  • Did you actually use taskAsJson in your code? When you write '...' + task + '...', i.e. performing string concatenation, then the object task will be converted to a string. The default string representation of an object is [object Object], which in your case leads to the string <a href="#" onclick="showTask([object Object]);">. This doesn't look like proper JS, does it? Commented Sep 29, 2013 at 21:47

2 Answers 2

7

JavaScript in HTML is bad. HTML in JavaScript is bad. JavaScript in HTML in JavaScript? You get problems like these.

Make the element the right way.

var items = $.map(tasks, function(task) {
    return $('<li>').append(
        $('<a>', {
            href: '#',
            text: task.Description,
            click: function(e) {
                e.preventDefault();
                showTask(task);
            }
        })
    );
});
Sign up to request clarification or add additional context in comments.

3 Comments

+1 for the tips. I tried this and now each <li> in my listview is blank. And when showTask() receives a task, task.Description shows as undefined. I'm trying to work through it...
@BobHorn: My bad — got the argument order to $.map backwards :) It should be fixed now.
I was just about to edit your answer because I found that same issue here: api.jquery.com/jQuery.map. Thanks!
3

The issue is that showTask(' + task + '); lacks quotes inside the JavaScript code. For example, if task would be "cars" the HTML created will be onclick="showTask(cars)". Now "cars" lacks quotes. In addition, in your example task probably isn't even a string but an object, so you are looking for something like task.id to get the right parameter. Otherwise task's string representation will be [object Object].

It works when you hardcode 5 because 5 is a number value that does not require quotes. (onclick="showTask(5)" is valid)

So if you change it to showTask(\'' + task + '\'); it should work because the escaped \' will be ' in the final HTML code leading to onclick="showTask('valueOfTask')" (at least if task is the string you want in the parameter). But, as minitech pointed out, it's generally bad to put JS code in HTML.

1 Comment

+1 for the tip. Thank you. Couldn't get it to work, so I'm trying the other answer at the moment.

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.