Questions tagged [javascript]
For questions about programming in ECMAScript (JavaScript/JS) and its different dialects/implementations (except for ActionScript). Note that JavaScript is NOT Java. Include all tags that are relevant to your question: e.g., [node.js], [jQuery], [JSON], [ReactJS], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
javascript
192,486
questions
6709
votes
42
answers
2.1m
views
How do I return the response from an asynchronous call?
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 ...
1674
votes
23
answers
1.1m
views
Event binding on dynamically created elements?
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 ...
1914
votes
16
answers
636k
views
How to access the correct `this` inside a callback
I have a constructor function which registers an event handler:
function MyConstructor(data, transport) {
this.data = data;
transport.on('data', function () {
alert(this.data);
...
3248
votes
45
answers
484k
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);
...
916
votes
7
answers
305k
views
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
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....
611
votes
7
answers
202k
views
Why does jQuery or a DOM method such as getElementById not find the element if I put the script element near the top of the page?
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 ...
1135
votes
32
answers
1.3m
views
How can I access and process nested objects, arrays, or JSON?
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,
...
63
votes
1
answer
60k
views
Official locator strategies for the webdriver
In the official W3C webdriver documentation, it's clearly stated that the location strategies are:
State Keyword
-----------------------------------------------
CSS selector ...
572
votes
3
answers
58k
views
What is the difference between client-side and server-side programming?
I have this code:
<script type="text/javascript">
var foo = 'bar';
<?php
file_put_contents('foo.txt', ' + foo + ');
?>
var baz = <?php echo 42; ?>;
...
1496
votes
22
answers
468k
views
How does the "this" keyword work, and when should it be used?
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 ...
207
votes
12
answers
107k
views
What do querySelectorAll() and getElementsBy*() methods return?
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 ...
7615
votes
86
answers
1.6m
views
How do JavaScript closures work?
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 ...
652
votes
3
answers
143k
views
What is the explicit promise construction antipattern and how do I avoid it?
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) {
...
978
votes
21
answers
663k
views
Accessing an object property with a dynamically-computed name
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.foo; // The idea is to access something.bar, ...
863
votes
22
answers
857k
views
The useState set method is not reflecting a change immediately
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 ...