2

I have this line that appending this in jquery:

$('#league2').append("<input type=\"button\" id=\"2btn\" value=\"1.2\" class=\"butta\"  onmousedown=\"active('exam\'ple','awayteam')");

Notice the "exam\'ple"... i escaped the ' with \' so when clicking the button, the function active should work.

this is the function active:

function active(hometeam,awayteam){
alert("if this is alerted, it works!");
}

when i click the button it should alert "if this is alerted, it works!", but it's not alerting it. and it think because when i use the function, this is the outpot:

function active(exam\'ple,awayteam){

when i appending the same thing with a word that does not contain " ' ", it is working.

2 Answers 2

2

You need to escape the backslash in your parameters for the active function, instead of the apostrophe.

$('#league2').append("<input type=\"button\" id=\"2btn\" value=\"1.2\" class=\"butta\"  onmousedown=\"active('exam\\'ple','awayteam')");

To escape a string to append it to that code with php, you can use regular expressions. The following will work in your case.

// Replaces a backslash with four backslashes: The 'append' function will interpret it as two backslashes; the 'active' function, as only one.
$str = preg_replace("/\\\\/", "\\\\\\\\\\\\\\\\", $str);
// Replaces an apostrophe with two backslashes and an apostrophe, then javascript will append only one backslash and an apostrophe, escaping that way an apostrophe.
$str = preg_replace("/'/", "\\\\\\\\'", $str);
// Replaces quotations with a backslash and quotations: the quotations will be escaped inside the append function, so one quotations will be printed out on your HTML
$str = preg_replace("/\"/", "\\\\\"", $str);

If you don't know what a regular expression (regex) is, I suggest you researching a bit about how to use them. They will help you a lot.

EDIT: By some reason, the last program needed double of backslashes to work. Updated.

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

6 Comments

Works!! thank you, but i need a little bit more help. lets say i'm getting this exam'ple from a php mysql database. so the varriable is stored in $test how do i echo exam\'ple like this "exam\\'ple"?
So you're getting the strings from a database and then appending them to the HTML using php, right?
yes. how do i add the 2 backslashes before the " ' " using php?
i saw it, and i am getting an error when using the first example Warning preg_replace(): No ending delimiter '/' found in ..... on line 243
Yeah. I don't know why php interprets backslashes on preg functions in a weird way. Updated the answer, at least it now works on my computer.
|
1

You don't need to escape single quotes when the string is double-quoted. It's best practice to wrap the whole string in single quotes, allowing you to use double quotes without the need to escape. (or single quotes with it all wrapped up in doubles).

For example:

$("#thing").append('<div id="foo" class="bar" style="color:black;" onclick="foobar()"></div>');

Your code's a little more complicated, because there's multiple levels (have you considered jQuery's click?):

$('#league2').append('<input type="button" id="2btn" value="1.2" class="butta"  onmousedown="active('exam\'ple','awayteam')');

1 Comment

This is more of a best practice. Escaping quotes in strings is very ugly and is a last resort. +1

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.