Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6994219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:51:51+00:00 2026-05-27T19:51:51+00:00

function test() { var id = 123; var id12 = ‘abcd’; var childDiv =

  • 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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:51:52+00:00Added an answer on May 27, 2026 at 7:51 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

why does this only alert 1 ? function test() { var myobj = {
function test(){ if(true){ var a = 5; } alert(a); } test(); I keep getting
I'm using the following code.. $(document).ready(function(){ $(#test a).click(function(){ var labelTo = $(this).text(); window.location =
var x= 1; Number.prototype.test = function () { return this }; x.test() === x.test()
function check(id){ var _id = document.getElementById(id); url = test.php?check=1&id= + _id; } i want
I have the following dummy test script: function test() { var x = 0.1
I have a problem with this fonction: function test(value){ var id = ' +
i have this example: <script> $('.myvideos').live('click', function() { $('#myvideos').trigger('click'); var ide = '123'; function
If I do function test() { var fromDate=01/17/2012; var res= Test1(fromDate); return false; }
Hi i have this javascript function function test(){ var count = 0; var date1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.