3

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?

1
  • 2
    Have a look at the syntax highlighting, and you should be able to see the problem... Commented Apr 8, 2012 at 14:46

2 Answers 2

11

You'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.

Sign up to request clarification or add additional context in comments.

9 Comments

+1 Although I'd recommend he/she create the element and attach it instead.
+1 But, why not open and close with double quotes and leave the original single quotes alone?
Hanks alot bro @BenGriffiths you are the best
@Rob W: $20 says that since his question is answered he will never see this page again.
@jmort253 - My response was to the comment asking the OP to accept an answer.
|
8

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

1 Comment

This is the cleaner solution. The quotes within quotes within quotes, to the point where you have to escape them, can get pretty complicated after awhile. +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.