Skip to main content
6744 votes
44 answers
2.2m views

How do I return the response/result from a function foo that makes an asynchronous request? I am trying to return the value from the callback, as well as assigning the result to a local variable ...
Felix Kling's user avatar
1673 votes
23 answers
1.1m views

I have a bit of code where I am looping through all the select boxes on a page and binding a .hover event to them to do a bit of twiddling with their width on mouse on/off. This happens on page ready ...
Eli's user avatar
  • 100k
1923 votes
16 answers
658k views

I have a constructor function which registers an event handler: function MyConstructor(data, transport) { this.data = data; transport.on('data', function () { alert(this.data); ...
Felix Kling's user avatar
3263 votes
45 answers
498k views

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
  • 548k
919 votes
7 answers
310k views

Given the following examples, why is outerScopeVar undefined in all cases? var outerScopeVar; var img = document.createElement('img'); img.onload = function() { outerScopeVar = this.width; }; img....
Fabrício Matté's user avatar
618 votes
7 answers
209k views

What are the possible reasons for document.getElementById, $("#id") or any other DOM method / jQuery selector not finding the elements? Example problems include: jQuery silently failing to ...
Felix Kling's user avatar
1142 votes
31 answers
1.3m views

I have a nested data structure containing objects and arrays. How can I extract the information, i.e. access a specific or multiple values (or keys)? For example: var data = { code: 42, ...
Felix Kling's user avatar
64 votes
1 answer
65k views

In the official W3C webdriver documentation, it's clearly stated that the location strategies are: State Keyword ----------------------------------------------- CSS selector ...
Merc's user avatar
  • 17.2k
574 votes
3 answers
61k views

I have this code: <script type="text/javascript"> var foo = 'bar'; <?php file_put_contents('foo.txt', ' + foo + '); ?> var baz = <?php echo 42; ?>; ...
deceze's user avatar
  • 525k
1500 votes
22 answers
482k views

I am looking to find a clear explanation of what the "this" keyword does, and how to use it correctly. It seems to behave strangely, and I don't fully understand why. How does this work and ...
Maxim Gershkovich's user avatar
211 votes
11 answers
111k views

Do getElementsByClassName (and similar functions like getElementsByTagName and querySelectorAll) work the same as getElementById or do they return an array of elements? The reason I ask is because I ...
dmo's user avatar
  • 5,391
7609 votes
86 answers
1.6m views

How would you explain JavaScript closures to someone with a knowledge of the concepts they consist of (for example functions, variables and the like), but does not understand closures themselves? I ...
662 votes
3 answers
151k views

I was writing code that does something that looks like: function getStuffDone(param) { return new Promise(function(resolve, reject) { myPromiseFn(param+1) .then(function(val) { ...
Benjamin Gruenbaum's user avatar
995 votes
22 answers
687k views

I'm trying to access a property of an object using a dynamic name. Is this possible? const something = { bar: "Foobar!" }; const foo = 'bar'; something.⟨value of foo⟩; // The idea is to ...
RichW's user avatar
  • 11k
908 votes
21 answers
929k views

I am trying to learn hooks and the useState method has made me confused. I am assigning an initial value to a state in the form of an array. The set method in useState is not working for me, both with ...
Pranjal's user avatar
  • 9,193

15 30 50 per page
1
2 3 4 5
13139