hh54188
hh54188

Reputation: 15656

The "illegal character" in jquery appendTo()

here is my jquery appendTo() code:

$('<li><a href="javascript:void (0)" onmousedown="document.getElementById('searchType_banner').value='p';">document</a></li>').appendTo('.wapper');

in firebug it remind me an error that "missing ) after argument list" in "onmousedown" position,and sometimes it remind me that "illegal character" error.But I don't think I miss any ")" .

So what should not appear in the appendTo() or which I should noticed in appendTo() .Why the error above happened,how can I solve this problem?

thank you

Upvotes: 0

Views: 1157

Answers (3)

Phrogz
Phrogz

Reputation: 303510

If you look at the syntax highlighting on your own Stack Overflow question I think you can see what's going wrong. Notice where the string ends.

Here's how to fix it:

$('<li><a href="javascript:void (0)" onmousedown="document.getElementById(\'searchType_banner\').value=\'p\';">document</a></li>').appendTo('.wapper');

Alternative fix:

$('<li><a href="javascript:void (0)" onmousedown="document.getElementById(&quot;searchType_banner&quot;).value=&quot;p&quot;;">document</a></li>').appendTo('.wapper');

Editorial: Ugh! You should not use anchor elements just for JavaScript. Use a <button> and style it like a link if you like. Here's how I'd write that:

$('<button>document</button>').mousedown(function(){
  $('#searchType_banner').val('p');
}).appendTo('.wapper');

Upvotes: 1

Irving
Irving

Reputation: 1

$('<a></a>').html('document').mousedown(function(){}).wrap('<li></li>').appendTo('.wapper');

Upvotes: 0

Robert
Robert

Reputation: 21388

The selector is essentially a string, so you're breaking your string with the single quotes. You need to escape them.

$('<li><a href="javascript:void (0)" onmousedown="document.getElementById(\'searchType_banner\').value=\"p\";">document</a></li>').appendTo('.wapper');

Upvotes: 1

Related Questions