admin管理员组文章数量:1318331
I have an issue with onclick
function.
Here's my code :
$('ul.listing').append('<button onclick="openInApp("test")" class="btn btn-primary">Edit <i class="fa fa-external-link" aria-hidden="true"></i></button>');
When I click the button, I have the "Unexpected end of input
" error message.
I try to pass variable too like this :
$('ul.listing').append('<button onclick="openInApp('+ edit_url +')" class="btn btn-primary">Edit <i class="fa fa-external-link" aria-hidden="true"></i></button>');
And I get the following message : Uncaught SyntaxError: missing ) after argument list
Both errors begins at the "("
I have another function (always in the append) :
$('ul.listing').append('<a href="event-details.html" onclick="saveEventID(' + data[i].ID + ');">')
And it works perfectlly
I don't understand
I have an issue with onclick
function.
Here's my code :
$('ul.listing').append('<button onclick="openInApp("test")" class="btn btn-primary">Edit <i class="fa fa-external-link" aria-hidden="true"></i></button>');
When I click the button, I have the "Unexpected end of input
" error message.
I try to pass variable too like this :
$('ul.listing').append('<button onclick="openInApp('+ edit_url +')" class="btn btn-primary">Edit <i class="fa fa-external-link" aria-hidden="true"></i></button>');
And I get the following message : Uncaught SyntaxError: missing ) after argument list
Both errors begins at the "("
I have another function (always in the append) :
$('ul.listing').append('<a href="event-details.html" onclick="saveEventID(' + data[i].ID + ');">')
And it works perfectlly
I don't understand
Share Improve this question edited May 14, 2018 at 18:01 Jean R. asked May 14, 2018 at 17:48 Jean R.Jean R. 5444 gold badges18 silver badges46 bronze badges 5-
2
Since you used
onclick="openInApp("test")"
, only"openInApp("
is passed to the onclick listener – Luca Kiebel Commented May 14, 2018 at 17:52 - 3 Your problem is conflicting quotes, try to avoid using attribute event handlers, these are just one of several issues that e up when using them. – Musa Commented May 14, 2018 at 17:54
- I know that the probleme is the quotes but how to solve it ? I try to inverse double quote and quote, to remove it, etc but still the same problem. Also why the other function worked ? – Jean R. Commented May 14, 2018 at 17:59
- 2 You can escape the quotes with a backspace, or use a string template, or you can bind the event handler to the element as a non-inline binding. There are various ways to approach this issue. – Taplar Commented May 14, 2018 at 18:02
-
Just don't use inline event attributes at all. Bind the function using jQuery
on
. – Bergi Commented May 14, 2018 at 18:07
1 Answer
Reset to default 5To nest the string in the onclick, you can do it like below where the single quote is escaped using a backslash to prevent it from terminating the string.
$('ul.listing').append('<button onclick="openInApp(\'test\')" class="btn btn-primary">Edit <i class="fa fa-external-link" aria-hidden="true"></i></button>');
An alternative way to handle this can be done by passing the string through a separate data attribute:
$('ul.listing').append('<button onclick="openInApp(this)" data-string="yourstringhere" class="btn btn-primary">Edit <i class="fa fa-external-link" aria-hidden="true"></i></button>');
and then accessing it in the onclick function like this:
function openInApp(element) {
var mystring = $(element).data("string");
}
本文标签: javascriptquotUncaught SyntaxError Unexpected end of inputquot with onclickStack Overflow
版权声明:本文标题:javascript - "Uncaught SyntaxError: Unexpected end of input" with onclick - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742041938a2417588.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论