0
function test() {
    var id = 123;
    var id12 = 'abcd';
    var childDiv = "";
    childDiv += '<div style="width:20px;height:20px"  onclick="newsClicked('+id12+')">Click</div>';  

    $("#mydiv").append(childDiv);

}

function newsClicked(param) {
    alert(param);
}

Above is the test function, test() is the function which I call inside onClick event of a test div. If I pass id which is a number(integer) it works, If I pass string it says reference error. Its related to closure I guess. How to solve it. Whats happening actually here ?

3 Answers 3

2

Change:

childDiv += '<div style="width:20px;height:20px" onclick="newsClicked('+id12+')">Click</div>';  

to

childDiv += '<div style="width:20px;height:20px" onclick="newsClicked(\''+id12+'\')">Click</div>';  

When you append childDiv 1st one to div with integer as parameter the result will be:

<div style="width:20px;height:20px"  onclick="newsClicked(3)">Click</div>

which is correct, in case of string the result will be:

<div style="width:20px;height:20px"  onclick="newsClicked(string_as_arg)">Click</div>

this is not ok, because string_as_arg is not a variable at last not in the scope. That's why you should enclose it in apostrophes. Then the proper results will be:

<div style="width:20px;height:20px"  onclick="newsClicked('3')">Click</div>
<div style="width:20px;height:20px"  onclick="newsClicked('string_as_arg')">Click</div>

so there is no really big difference in case of integer constant but string now is properly recognised as string not as variable.

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

2 Comments

Thanks , Got it working, but I did not get what exactly is the problem, To pass as a string parameter, we need to escape it , why ?
@sat you are passing this as a string but only to concatenate different statement of code which will be executed later.
1

Its working when you are passing a number because the number need not be in quotes but the string has to be in quotes. so to pass string you should use escape char "\"

something like :

newsClicked(\''+id12+'\')

Comments

1

on you issues,

How to solve it.

*note: you can see like \''+id12+'\' on below code

function test() {
    var id = 123;
    var id12 = 'abcd';
    var childDiv = "";
    childDiv += '<div style="width:20px;height:20px"  onclick="newsClicked(\''+id12+'\')">Click</div>';  

    $("#mydiv").append(childDiv);

}

function newsClicked(param) {
    alert(param);
}

Whats happening actually here ?

newsClicked function will fired after DIV clicked which is recently added to dom, It will added to dom like

<div onclick="newsClicked(abcd)" style="width: 20px; height: 20px;">Click</div>

here, abcd is use like variable, but does not exist on real.

And you will get error like

abcd is not defined

but if you put id variable which is integer, it will add on dom like

<div onclick="newsClicked(123)" style="width: 20px; height:20px;">Click</div>

which is totally correct on syntax wise.

I guess, you clear on this now.

Cheers,

1 Comment

ok, so If integer is passed, it takes as value, not as variable, thats why it works! Phew!

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.