最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - "Uncaught SyntaxError: Unexpected end of input" with onclick - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 5

To 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");
}
发布评论

评论列表(0)

  1. 暂无评论