Linked Questions

3242 votes
45 answers
482k views

JavaScript closure inside loops – simple practical example

var funcs = []; // let's create 3 functions for (var i = 0; i < 3; i++) { // and store them in funcs funcs[i] = function() { // each should log its value. console.log("My value:", i); ...
nickf's user avatar
  • 542k
22 votes
5 answers
25k views

scope of variables in JavaScript callback functions

I expected the code below to alert "0" and "1", but it alert "2" twice. I don't understand the reason. Don't know if it is a problem of jQuery. Also, please help me to edit title and tags of this ...
Ethan's user avatar
  • 18.8k
19 votes
2 answers
14k views

using anonymous function in javascript for loops

I have seen anonymous functions inside for loops to induce new scope on the web in one or two places and would like to know if it makes sense. for example: var attr, colors = ['green','blue','red']; ...
Evan's user avatar
  • 6,105
8 votes
2 answers
7k views

javascript scope problem when lambda function refers to a variable in enclosing loop

First question on stackoverflow :) Hope I won't embarrass myself... I have a javascript function that loads a list of albums and then it creates a list item for each album. The list item should be ...
TV's Frank's user avatar
1 vote
5 answers
6k views

Dynamic onclick function assignment with dynamic parameters in JavaScript?

I have this code: document.getElementById('img'+i).onclick = function(){popup_show('popup',array_msg[i]+'|||'+date('Y-m-d',strtotime(lec_date))+'-==-'+str_view_forConflict, 'AddEditSchedule;;...
Treby's user avatar
  • 1,320
1 vote
3 answers
18k views

" document.getElementById onmouseover and function " does not behave as wished × 108641

In this function, it should give the menu items (li's) an specific background (png) out of an array. However; it doesn't. It gives all the li's the background called color 'blue' :(. Do you see the ...
jroeleveld's user avatar
4 votes
2 answers
2k views

JavaScript closures and variable scope

I am having trouble with JS closures: // arg: an array of strings. each string is a mentioned user. // fills in the list of mentioned users. Click on a mentioned user's name causes the page to load ...
Nick Heiner's user avatar
1 vote
4 answers
2k views

Dynamically Change HTML DOM event

I am trying to dynamically change an element's onClick event and I have something like the following: for (var i = 1; i < 5; i++) { getElementById('element' + i).onclick = function() { ...
yogibear's user avatar
  • 14.7k
3 votes
2 answers
2k views

How can I pass a custom argument to an event listener in JavaScript?

How can I add an event listener (addEventListener(),attachEvent()) that also accepts a parameter? The parameter is passed as the element's custom attribute like: <img src="icon.gif" alt="Test ...
samuel's user avatar
  • 321
2 votes
5 answers
619 views

JavaScript Variable Scope [duplicate]

I'm having a problem with some JavaScript code. Script setTimeout(function() { for (var i = 0; i < 5; i++) { setTimeout(function() { console.log(i); }, i * 200); ...
Michael Waterfall's user avatar
0 votes
1 answer
1k views

Youtube Player Internet Explorer Malfunction

On demo I am trying to develop a chromeless Youtube player. Control links on that link works fine on Firefox and Chrome but doesn't work on Internet Explorer. How can i fix this problem? I think ...
mcan's user avatar
  • 2,012
3 votes
1 answer
1k views

JavaScript: Passing changing parameters to a callback

Now here's a fun problem. I have an object array as the following: objRequests = [ { url: "/cgi-bin/script1.cgi", dest: "#div1" }, { url: "/cgi-bin/script1.cgi", dest:...
RandomInsano's user avatar
  • 1,242
1 vote
1 answer
1k views

gmaps.js - adding a click function for each marker

I'm using the Gmaps.js api in a little project I'm building and struggling with adding click events within a for loop. Here's the code: for(var i = 0; i < number; i++) { var entryTime = ...
Lukas's user avatar
  • 13
3 votes
2 answers
365 views

Creating a function reference that has value parameters not references

I'm not sure exactly how to describe what I want. I want to define a function with the parameters being a local VALUE not a reference. say I have list of objects I want to create for(i = 0; i < ...
Sheldon Ross's user avatar
  • 5,474
0 votes
1 answer
1k views

loadByvideoId is not a function

The below fiddle when opened in chrome,fox and iE its working fine 'http://jsfiddle.net/wmdPN/' When i copy the code from the fiddle and use it in my machine,its not working i m getting error as o....
dinesh babu's user avatar

15 30 50 per page