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?
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?
Share Improve this question edited Apr 8, 2012 at 15:06 Gary 13.9k18 gold badges53 silver badges72 bronze badges asked Apr 8, 2012 at 14:44 BerakiBeraki 1421 gold badge2 silver badges15 bronze badges 1- 2 Have a look at the syntax highlighting, and you should be able to see the problem... – Rob W Commented Apr 8, 2012 at 14:46
2 Answers
Reset to default 11You're not escaping the single quotes:
$('span.xtro').html('<input type="button" class="newbutton send" value="Send request"'
+ ' onclick="request(\'send\',\'1\',\'2\');">');
You can also get rid of the first $('span.xtro').html('');
, you shouldn't need it.
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