Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
157
rated 0 times [  162] [ 5]  / answers: 1 / hits: 39918  / 12 Years ago, sun, april 8, 2012, 12:00:00

The following gives me the error Uncaught SyntaxError: Unexpected identifier:



$('span.xtro').html('');
$('span.xtro').html('<input type='button' class='newbutton send' value='Send request' onclick=
javascript:request('send','1','2');'>');


How can I correct this?


More From » jquery

 Answers
21

It's not the best way of using jQuery... onclick attributes are not recommended. here's an alternative



//note wrapping with double quotes and using single ones inside 
var $el = $( <input type='button' class='newbutton send' value='Send request'> );
$el.on( 'click', function(){ request('send','1','2'); } );
$('span.xtro').html('').append( $el );


EDIT changed $el.bind to $el.on which is what is used nowadays


[#86369] Thursday, April 5, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tomas

Total Points: 165
Total Questions: 111
Total Answers: 103

Location: Maldives
Member since Tue, Dec 21, 2021
2 Years ago
tomas questions
Thu, Jan 27, 22, 00:00, 2 Years ago
Mon, May 10, 21, 00:00, 3 Years ago
Tue, Jan 5, 21, 00:00, 3 Years ago
;