Arnas Marnas Arnas Marnas - 1 year ago 63
Javascript Question

Error with HTML/JS

I'm trying to call function

after pressing on
, but I don't understand what happens. My code:

var something = "123456789";
swal({ text: "<a href='#' onclick='test('"+something+"', "1")'>Link</a>" });


<a href="#" onclick="test(" 123456789',="" "1")'="">Link</a>

What I want to get:

<a href='#' onclick='test("123456789", "1")'>Link</a>

As you can see strange
appears. Also, this happens in PHP too. How to fix that?

Answer Source

You have to escape the double quotes:

var something = "123456789";
alert("<a href='#' onclick='test(\"" + something + "\",\"1\")'>Link</a>");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download