0

Consider this line of JavaScript

str += "onclick=runTest(" +  field + "," + atom[0] + ",'" + atom[1] + "'); 

In the browser, it renders as:

onclick="runTest(104,213,'Orsan" Miller');

There is an inverted comma between Orsan and Miller although in fact there was not any inverted comma, it's causing a bug.

atom[1] = Orsan Miller

'Orsan Miller' comes from DB query in PHP.

How can this be fixed?

3
  • An "inverted comma" is also known as a quotation mark :P Commented Nov 21, 2011 at 21:58
  • is your first line of javascript a typo or are you really missing the closing quote? Commented Nov 21, 2011 at 21:58
  • @brianreavis And if you lived in Britain, you'd call it an "inverted comma". Commented Nov 21, 2011 at 22:00

2 Answers 2

2

You're missing some quotation marks and escaping... try this:

str += "onclick=\"runTest(" +  field + "," + atom[0] + ",'" + atom[1] + "')\""; 

I prefer to use single quotes for readability, but that's just me:

str += 'onclick="runTest(' + field + ',' + atom[0] + ',\'' + atom[1] + '\')"';
Sign up to request clarification or add additional context in comments.

Comments

0
str += "onclick=runTest(" +  field + "," + atom[0] + ",'" + atom[1] + "')"; 

Is what you are looking for, it was fine except you forgot the final double quote.

Comments

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.