3

This kinda funny, i want to quote a string value for a function but javascript throws this error Uncaught SyntaxError: Unexpected token }

This my code

var val = "Testing String";
var table_row = "<tr><td><a href='#' onclick='test('"+val+"')'>Row1</a></td></tr>";

function test( val ){
  alert( val );
}

The table row get created fine with the test function bound well with onClick event. But when i click on the created link i get

`Uncaught SyntaxError: Unexpected token }

The Value within the test function should be a quoted string.

Note: If i remove the concatenation of the val string, and pass is as a string like this

table_row = "<tr><td><a href='#' onClick='test(\"Testing String\")' >Row1</a></td>";

... it works

Where am i goofing?

Gath.

1
  • Does "..."Row1"..." really work? Commented Jul 11, 2011 at 13:25

4 Answers 4

1

Then in your original script also escape ":

var test = "\"test string\"";
Sign up to request clarification or add additional context in comments.

1 Comment

The original string (first line) does not contain any quotation marks to be escaped:
1

i thing it ll help u sir,,,,

if row1 is variable

u can add by '+Row1+' seperator,

or else directly use bellow code;

var table_row = '<tr><td><a href="#" onClick="test(\'Testing String\')" >Row1</a></td>';
        //alert("++++++++++++"+table_row+"++++++++++++");

see ya ,,,

Comments

0

Given

var val = "Testing String";
var table_row = "<tr><td><a href='#' onclick='test('"+val+"')'>Row1</a></td></tr>";

Should table_row not be

"<tr><td><a href='#' onclick='test('Testing String')'>Row1</a></td></tr>"

so the onclick attribute is only 'test(', because ' ends it?

Try:

var table_row = "<tr><td><a href='#' onclick=\"test('"+val+"')\">Row1</a></td></tr>";

Comments

0

This what i did and now working fine;

var val = "Testing String";
table_row = "<tr><td><a href='#' onclick='test(\""+val+"\")'>Row1</a></td></tr>";

function test( val ){
 alert( val)   // Displays "Testing String"
}

Thanks for your patience.

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.