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

  • Home
  • SEARCH
  • 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 7975373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:37:28+00:00 2026-06-04T08:37:28+00:00

I am looking at a javascript code that manipulates an HTML A tag ,

  • 0

I am looking at a javascript code that manipulates an HTML A tag , and I’m having trouble understanding how it sets up the “onclick” property. It seems to be telling it to update ytplayer_playitem with the index variable j and then call ytplayer_playlazy(1000)

But what’s up with all the parentheses? What details in the javascript syntax allows it to be setup like this?

var a = document.createElement("a");
a.href = "#ytplayer";
a.onclick = (function (j) {
    return function () {
        ytplayer_playitem = j;
        ytplayer_playlazy(1000);
    };
})(i);
  • 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-06-04T08:37:29+00:00Added an answer on June 4, 2026 at 8:37 am

    Well, basically, the value of onclick is a function that will get called when the element is clicked. Whatever you want to happen when the user clicks the element goes in the body of the function.

    You could create a named function and then assign it to the element’s onclick attribute:

    function youClickedMe() {
      ...
    }
    a.onclick = youClickedMe
    

    but that clutters up the namespace with a function name that is never referenced anywhere else. It’s cleaner to create an anonymous function right where you need it. Normally, that would look like this:

    a.onclick = function() { ... }
    

    But if we try that with your specific example:

    a.onclick = function() { 
      ytplayer_playitem = something; // ??
      ytplayer_playlazy(1000); 
    }
    

    We see that it hard-codes the something that gets played. I’m assuming the original code was taken from a loop which generates several clickable links to play; with the code just above, all of those links would play the same thing, which is probably not what you want.

    So far, so straightforward, but this next leap is where it gets tricky. The solution seems obvious: if you’re in a loop, why not just use the loop variable inside the function body?

    // THIS DOESN'T WORK
    a.onclick = function() { 
      ytplayer_playitem = i; 
      ytplayer_playlazy(1000); 
    }
    

    That looks like it should work, but unfortunately the i inside the function refers to the value of the variable i when the function is called, not when it’s created. By the time the user clicks on the link, the loop that created all the links will be done and i will have its final value – probably either the last item in the list or one greater than that item’s index, depending on how the loop is written. Whatever its value is, you once again have the situation where all links play the same item.

    The solution in your code gets a little meta, by using a function whose return value is another function. If you pass the loop control variable to the generating function as an argument, the new function it creates can reference that parameter and always get the value that was originally passed in, no matter what has happened to the value of the outer argument variable since:

    function generate_onclick(j) {
        // no matter when the returned function is called, its "j" will be 
        // the value passed into this call of "generate_onclick"
        return function() { ytplayer_playitem = j; ytplayer_playlazy(1000); }
    }
    

    To use that, call it inside the loop like this:

    a.onclick = generate_onclick(i);
    

    Each generated function gets its very own j variable, which keeps its value forever instead of changing when i does. So each link plays the right thing; mission accomplished!

    That’s exactly what your posted original code is doing, with one small difference: just like the first step in my explanation, the author chose to use an anonymous function instead of defining a named one. The other difference here is that they are also calling that anonymous function immediately after defining it. This code:

    a.onclick = (function (j) { ... })(i)
    

    is the anonymous version of this code:

    function gen(j) { ... }
    a.onclick = gen(i)
    

    The extra parens around the anonymous version are needed because of JavaScript’s semicolon-insertion rules; function (y) {...}(blah) compiles as a standalone function definition followed by a standalone expression in parentheses, rather than a function call.

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

Sidebar

Related Questions

So I'm looking at some code from Eloquent Javascript and it says that to
I'm looking for code that allows me to use JavaScript to load another JavaScript
I am looking for some sample JavaScript code that will allow me to have
I was looking over the JavaScript source code for SlickGrid . I've noticed that
I'm a beginner w/ Javascript. I'm looking at the following code that someone else
I've been looking at other people's JavaScript code, and I've noticed that many programmers
I'm looking for javascript code that will return the language selected by the current
I am looking for javascript code that display marker on top of others. May
I'm looking for Javascript code for letting the user draw a line (on an
To debug some JavaScript code, I am looking for JavaScript code (preferably just js,

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.