0

I have a js file where I need to construct some HTML the problem is it needs to be in a string to be parsed. Something like;

var myHTML="<a href='"#"' onclick='"alert("hello"); event.returnValue = false; return false;"'>"+childOrders+"</a>"

I have tried lots of combinations of quotes but just cannot get it to work.

Thanks for the help, Chris

1
  • 2
    Exciting! Which answer will he accept? Commented Sep 21, 2012 at 8:04

7 Answers 7

1

try following code:

var myHTML="<a href='#' onclick='alert(\"hello\"); event.returnValue = false; return false;'>childOrders</a>"

jsfiddle

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

Comments

0

You can escape quotes within a string by putting a backslash in front of them, like so:

myString = "A string with \"quotes\" in it.";

The above will result in the string A string with "quotes" in it.

EDIT: As for the quotes inside the onclick attribute, I would suggest using single quotes, so your code would look like this:

var myHTML="<a href=\"#\" onclick=\"alert('hello'); event.returnValue = false; return false;\">"+childOrders+"</a>";

1 Comment

Thanks for this...worked just as I needed. Thanks to everybody who offered a solution!
0

I think you are looking for the mighty \ (backslash). Escape the quotes and it will work.

var myHTML="<a href=\"#\" onclick=\"alert(\"hello\"); event.returnValue = false; return false;\">"+childOrders+"</a>"

Comments

0
var myHTML = "<a href='#' onclick='alert(\"hello\"); event.returnValue = false; return false;'>" + childOrders + "</a>";

Comments

0

You need to escape the quotes inside the string like so

var myHTML="<a href=\"#\" onclick=\"alert(\'hello\'); event.returnValue = false; return false;\">childOrders</a>"

Comments

0
   var myHTML='<a href="#" onclick=alert("hello"); event.returnValue = false; return false;>'+childOrders+'</a>'

This works I think. Use the single quotes on the outside so you can still use the double quotes on the inside.

Comments

0

just like this

var myHTML="<a href='#' onclick='alert('hello'); event.returnValue = false; return false;'>"+1+"</a>"

Comments

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.