0

This is my code all I need to do is call a function which will write the contents to a dynamic div

<script language='javascript' type='text/javascript'>
function getComments(id)
{
  alert(id);
}
var resultSet="";
function CreateDiv()
{

resultSet+="<br/><div id='"+rows.data[i].id+"'></div><script language='javascript' type='text/javascript'> getComments("+rows.data[i].id+"); <\/script>";
}
window.onload=CreateDiv;
</script>

The function getComments is not being called at all

What's that I am missing here

5
  • Where is getComments supposed to be called?? It is not. Commented Jun 5, 2011 at 10:13
  • the above code block is written inside <body> tag, so as soon as the body loads the getComments should have been called, but its not getting called at all Commented Jun 5, 2011 at 10:16
  • lots of variables in the create div dont make sense Commented Jun 5, 2011 at 10:18
  • I format the JavaScript code. It now becomes obvious why the getComments function is not called. Hint: check the quotes. Commented Jun 5, 2011 at 10:19
  • I can NOT see the function being called. Please specify where. The fn name is only passed inside a string, which is assigned to a variable, which is used nowhere. Commented Jun 5, 2011 at 10:20

3 Answers 3

4

There are a few problems there.

  1. You're referencing rows without defining it anywhere, which will cause an exception.
  2. Assuming you define rows somewhere you haven't shown, the code's just creating a string containing a script tag and putting that string in resultSet. To cause the code inside the script tag to run, you'd have to assign resultSet to innerHTML on some element.
  3. There's an extra ) in your call to getComments within the generated script.

Separately: Your id values would appear to be numbers (this is based on your passing them into getComments with no quotes around them). Note that using id values starting with a digit is allowed in HTML5, but not in earlier versions of HTML and not in CSS, so it's probably best avoided.

There's almost certainly a better way to do what you're actually trying to do, you may consider a separate question outlining the goal, how you've approached it so far, and asking for alternatives.

Sign up to request clarification or add additional context in comments.

1 Comment

resultSet is defined: var resultSet=""; (above CreateDiv)
0

I would suggest that you break the code down into steps while you debug it. Specifically where you populate resultSet. Break it down at each plus sign. Then you can step through it and see how it is being populated.

resultSet+="<br/><div id='";
resultSet+=rows.data[i].id;

and so on.

Secondly, have a look in View Source to see what this looks like on the page when you run it. Does the HTML look properly formed?

Also, I am questioning whether that final <\/script> in resultSet is correct.

1 Comment

"Also, I am questioning whether that final <\/script> in resultSet is correct" It is, the resulting string will be "</script>" because escaping a slash in JavaScript just results in a slash. This is a common way of preventing the HTML parser from seeing "</script>" in the string and prematurely terminating the script block in which that string appears.
-1

Try replacing the createDiv function with this:

function CreateDiv(){
    resultSet += "<br/><div id='"+rows.data[i].id+"'></div>" + getComments(rows.data[i].id);
}

It should work flawlessly.

5 Comments

Did this actually work, @Sandhurst? I thought you would still have the issues with undefined variables that T.J. points out in his answer.
I fail to see why someone downvoted the only straight and acceptable answer.
You'll end up with the string "undefined" appearing in the resultSet string (since getComments doesn't return a value, so the result of the call to it is undefined). Better to just call it separately from appending to the string, if that's really the behavior the OP wants (and I assume it is, as this is the accepted answer).
@T.J. Crowder sure, but I'm assuming that the author was trying to write a function that returns a string from getComments. And also, var resultSet = ""; initiates the variable.
Fair enough (on the assumption). (I didn't say resultSet wasn't initialized; at least, not since an hour ago when Felix pointed out the line I missed.) BTW, I don't understand the downvote either, it seems inappropriate.

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.