-1

Anyone know why Tool_AJAX.loadString("' + split[i] + '"); is not working?
Even though this line works fine ..

split[i] = 'Hello'

alert('Tool_AJAX.loadString("' + split[i] + '")');
Output: Tool_AJAX.loadString("Hello");

Heres the troublesome code ..

body += 
    '<tr>' +
        '<td><h4><a href="javascript:;" onclick="Tool_AJAX.loadString("' + split[i] + '");">' + split[i] + '</a></h4></td>' +
        '<td>Dummy</td>' +
        '<td>Dummy</td>' +
    '</tr>';


Error: SyntaxError: syntax error
Source File: http://localhost:8080/Tool/
Line: 1, Column: 32
Source Code:
Tool_AJAX.loadString(

Tool_AJAX is defined as ..

var Tool_AJAX = {
    loadString: function(string){
0

3 Answers 3

1
'<td><h4><a href="javascript:;" onclick="Tool_AJAX.loadString("' + split[i] + '");">' + split[i] + '</a></h4></td>' +

This will end as

<td><h4><a href="javascript:;" onclick="Tool_AJAX.loadString("...");">...</a></h4></td>

The onclick-part is malformed because of the quotes.
If you can't separate the Javascript from the HTML use single quotes for the string between the brackets:

 '<td><h4><a href="javascript:;" onclick="Tool_AJAX.loadString(\'' + split[i] + '\');">' + split[i] + '</a></h4></td>' +
Sign up to request clarification or add additional context in comments.

3 Comments

I was in the middle of typing almost exactly this. :) +1
This works perfect, thanks! The reason I'm embedding the HTML inside JavaScript is because its dymanic, I don't know how many <tr> elements will be loaded into the split array. Do you have a suggestion on how this separation could still be achieved given this constraint?
I haven't talk about the HTML in the Javascript. I've talked about the inline handler for the onclick event. Why not add a click handler on the table instead of one for each row.
0

Try

Tool_AJAX.loadString("'" + split[i] + "'");

(Do you really need split[i] in quotes as argument?)

Comments

0

Try \"' + split[i] + '\". It should work.

1 Comment

Not quite. The problem isn't JavaScript; it's that HTML is interpreting the quotes. They need to be single quotes instead, or HTML-escaped like &quot;, so they don't cut off the attribute value.

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.