1

I'm getting syntax error when using this code:

$('#column1').append('<span class="link_style"><a href="#" onClick="show_text(' +  new_cities[i][j].content + ')"></a></span>');

When javascript executes I'm getting this code generated:

<a href="#" onclick="show_text(<p>description</p>)">Moscow</a>

and this would be the function which is producing error:

function show_text(text)
    {
        alert(text);
    }

new_cities[i][j].content has this value "<p>description</p>" so basically my data is represented as string so there is definitely some problem in the quotes....

1 Answer 1

4

you need to change this:

"show_text(' +  new_cities[i][j].content + ')"

into this :

"show_text(\'' +  new_cities[i][j].content + '\')"
Sign up to request clarification or add additional context in comments.

4 Comments

being that you need to add the single quotes, but to get them you need to escape them ()
can you explain a bit more about those "escaping" ? I would really like to understand it..and sure would others too. BTW. answer is correct
Yeah of course. "Escaping" as it's called is something you do when you want a characted to be returned or "echo'ed" but it's already used to set a string So this can be anything between single quotes or double quotes, if, like in your case, you want to actually display a single quote or double quote, you need to tell your code not to parse the following character, by putting a \ (forward slash) in front of it.
yeah sorry, i pressed enter in between :), i hope my explanation helps you out! Good luck with your project! Oh one little adendum is that this works in several scripting languages, actually as far as i know it works that way in all web languages..

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.