Arnas Marnas Arnas Marnas - 4 months ago 9
Javascript Question

Error with HTML/JS

I'm trying to call function

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

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


Output:

<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

You have to escape the double quotes:

var something = "123456789";
alert("<a href='#' onclick='test(\"" + something + "\",\"1\")'>Link</a>");