jquery - Am I escaping the strings incorrectly in JavaScript? - Stack Overflow

I am trying to create a clickable link in javascriptvar content = '<a href="#" oncl

I am trying to create a clickable link in javascript

var content = '<a href=\"#\" onclick=\"displayContent(\"TEST\")\">Whatever</a>';

$("#placeHolder").html(content);

but I keep getting an error

Uncaught SyntaxError: Unexpected token }

Is that not the correct way to escape double quotes and create a link?

I am trying to create a clickable link in javascript

var content = '<a href=\"#\" onclick=\"displayContent(\"TEST\")\">Whatever</a>';

$("#placeHolder").html(content);

but I keep getting an error

Uncaught SyntaxError: Unexpected token }

Is that not the correct way to escape double quotes and create a link?

Share Improve this question edited Jan 17, 2012 at 22:31 Alex Wayne 187k52 gold badges328 silver badges360 bronze badges asked Jan 17, 2012 at 22:26 CodeCrackCodeCrack 5,38311 gold badges47 silver badges75 bronze badges 5
  • 1 There's code missing here. There are no } characters in the code sample you've provided. – ceejayoz Commented Jan 17, 2012 at 22:28
  • Seems the cause is out of the scope of this snippet. – Dykam Commented Jan 17, 2012 at 22:30
  • You're escaping the double-quotes correctly at the very least – Sam I am says Reinstate Monica Commented Jan 17, 2012 at 22:30
  • @ceejayoz, it is most likely in the context of a function, but I also think there is relevant code missing. – Dykam Commented Jan 17, 2012 at 22:31
  • As @AdamRackis' answer shows, it's the " around the TEST argument. The onclick is going to end up looking like onclick="displayContent("TEST")" instead of onclick="displayContent('TEST')". – user1106925 Commented Jan 17, 2012 at 22:35
Add a ment  | 

4 Answers 4

Reset to default 7

You only need to escape the single quotes

var content = '<a href="#" onclick="displayContent(\'TEST\')">Whatever</a>'

As bozdoz says:

You escape single quotes that are within single quotes; you escape double quotes that are within double quotes


But why not do

var content = $("<a />").attr("href", "#").text("Whatever").click(function(){
     displayContent('TEST')
});

Or as Nathan says:

var content = $('<a href="#">Whatever</a>').click(
     function() { displayContent('TEST') 
});

You can avoid that mess by creating elements like this:

var content = $( "<a>", {
    href: "#",
    text: "whatever",
    click: $.proxy( displayContent, 0, "TEST" )
});

You only need to escape quotes when they are the same type as used by your opening and closing quotes. In your example, you are unnecessarily escaping double quotes because your string is wrapped in single quotes. That being said, because of the double quotes in onclick statement the parser will have issues with the the call to displayContent().

Try this, instead:

var content = '<a href="#" onclick="displayContent(\'TEST\')">Whatever</a>'; 

You don't need to escape them at all!

var content = '<a href="#" onclick="displayContent(\'TEST\')">Whatever</a>'; 

$("#placeHolder").html(content); 

Just have it like that, as you don't need to escape " usually, when in embeded in a ';

But you do need to escape ' characters when it is a parameter in a function.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744350300a4569932.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信