admin管理员组文章数量:1314584
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 unment 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');
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 unment 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');
Share
Improve this question
asked Sep 29, 2013 at 21:39
Bob HornBob Horn
34.3k36 gold badges120 silver badges235 bronze badges
1
-
Did you actually use
taskAsJson
in your code? When you write'...' + task + '...'
, i.e. performing string concatenation, then the objecttask
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? – Felix Kling Commented Sep 29, 2013 at 21:47
2 Answers
Reset to default 7JavaScript 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);
}
})
);
});
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.
本文标签: javascriptPassing variable in onclick Uncaught SyntaxError Unexpected indentifierStack Overflow
版权声明:本文标题:javascript - Passing variable in onclick: Uncaught SyntaxError: Unexpected indentifier - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741935350a2405823.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论