Reputation: 18649
I currently have this JavaScript:
new_string = "<p>Problem name: <a href=\"http://www.problemio.com/problems/problem.php?problem_id=\" + problem_id + ">" + title + "</a></p>";
Where the title and problem_id are variables with a string and an id respectively.
How should I approach quotes when I have such situations as I am facing now?
Thanks!
Upvotes: 1
Views: 170
Reputation: 5852
You can do this:
new_string = "<p>Problem name: <a href=\"http://www.problemio.com/problems/problem.php?problem_id=" + problem_id + "\">" + title + "</a></p>";
Or this:
new_string = '<p>Problem name: <a href="http://www.problemio.com/problems/problem.php?problem_id='+ problem_id +'">' + title + '</a></p>';
Upvotes: 0
Reputation: 78770
I believe you just have one quote out of place:
new_string = "<p>Problem name: <a href=\"http://www.problemio.com/problems/problem.php?problem_id=" + problem_id + "\">" + title + "</a></p>"
Upvotes: 1
Reputation: 5478
'
for string, "
for quotes inside string. I'm doing it like this, and I've seen most javascript projects do it that way too.
Upvotes: 2
Reputation: 82654
Just end it with a quote:
var variable = 0;
var string = "\"quoted\"" + variable + "\"quotes everywhere\""; // "quoted"0"quotes everywhere"
Upvotes: 2
Reputation: 1152
You've just made a few mistakes with your quotation marks and slashes. It's best to use both single and double quotes to keep track:
new_string = "<p>Problem name: <a href='http://www.problemio.com/problems/problem.php?problem_id=" + problem_id + "'>" + title + "</a></p>";
However, if you'd like to go the slash route:
new_string = "<p>Problem name: <a href=\"http://www.problemio.com/problems/problem.php?problem_id=" + problem_id + "\">" + title + "</a></p>";
Upvotes: 2