Questions tagged [javascript]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
I have an array of numbers and I'm using the .push() method to add elements to it.
Is there a simple way to remove a specific element from an array?
I'm looking for the equivalent of something like:
...
asked Apr 23 '11 at 22:17
Walker
112k2323 gold badges6565 silver badges9494 bronze badges
Is it possible to toggle the visibility of an element, using the functions .hide(), .show() or .toggle()?
How would you test if an element is visible or hidden?
asked Oct 7 '08 at 13:03
Philip Morton
124k3636 gold badges8585 silver badges9797 bronze badges
Recently, I ran some of my JavaScript code through Crockford's JSLint, and it gave the following error:
Problem at line 1 character 1: Missing "use strict" statement.
Doing some searching, I ...
asked Aug 26 '09 at 16:10
Mark Rogers
92.2k1818 gold badges8282 silver badges131131 bronze badges
How can I redirect the user from one page to another using jQuery or pure JavaScript?
asked Feb 2 '09 at 12:54
venkatachalam
102k2929 gold badges7070 silver badges7676 bronze badges
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 ...
Usually I would expect a String.contains() method, but there doesn't seem to be one.
What is a reasonable way to check for this?
I've recently started maintaining someone else's JavaScript code. I'm fixing bugs, adding features and also trying to tidy up the code and make it more consistent.
The previous developer used two ...
Say I create an object as follows:
let myObject = {
"ircEvent": "PRIVMSG",
"method": "newURI",
"regex": "^http://.*",
};
How should I ...
asked Oct 16 '08 at 10:57
johnstok
89.9k1212 gold badges5252 silver badges7676 bronze badges
I have a function foo which makes an asynchronous request. How can I return the response/result from foo?
I am trying to return the value from the callback, as well as assigning the result to a local ...
asked Jan 8 '13 at 17:06
Felix Kling
734k167167 gold badges10331033 silver badges10901090 bronze badges
Is there something in JavaScript similar to @import in CSS that allows you to include a JavaScript file inside another JavaScript file?
asked Jun 4 '09 at 11:59
Alec Smart
87.8k3535 gold badges116116 silver badges180180 bronze badges
I'm using JSLint to go through JavaScript, and it's returning many suggestions to replace == (two equals signs) with === (three equals signs) when doing things like comparing idSele_UNVEHtype.value....
asked Dec 11 '08 at 14:19
bcasp
57.4k44 gold badges1717 silver badges1414 bronze badges
ECMAScript 6 introduced the let statement.
I've heard that it's described as a local variable, but I'm still not quite sure how it behaves differently than the var keyword.
What are the differences?. ...
asked Apr 17 '09 at 20:09
TM.
99.4k3030 gold badges121121 silver badges127127 bronze badges
How can I loop through all the entries in an array using JavaScript?
I thought it was something like this:
forEach(instance in theArray)
Where theArray is my array, but this seems to be incorrect.
asked Feb 17 '12 at 13:51
Dante1986
54k1212 gold badges3535 silver badges5353 bronze badges
What is the most efficient way to clone a JavaScript object? I've seen obj = eval(uneval(o)); being used, but that's non-standard and only supported by Firefox. I've done things like obj = JSON.parse(...
What's the best way to validate an email address in JavaScript with a regular expression?
I have this string in my JavaScript code:
"Test abc test test abc test test test abc test test abc"
Doing:
str = str.replace('abc', '');
Seems to only remove the first occurrence of abc in ...
asked Jul 17 '09 at 17:53
Ali
243k253253 gold badges558558 silver badges742742 bronze badges
I need to check the checked property of a checkbox and perform an action based on the checked property using jQuery.
For example, if the age checkbox is checked, then I need to show a textbox to enter ...
I'm trying to create globally-unique identifiers in JavaScript. I'm not sure what routines are available on all browsers, how "random" and seeded the built-in random number generator is, ...
What is the most concise and efficient way to find out if a JavaScript array contains a value?
This is the only way I know to do it:
function contains(a, obj) {
for (var i = 0; i < a.length; i++...
asked Oct 25 '08 at 22:14
brad
69.6k2121 gold badges6969 silver badges8484 bronze badges
Suppose I'm familiar with developing client-side applications in jQuery, but now I'd like to start using AngularJS. Can you describe the paradigm shift that is necessary? Here are a few questions that ...
asked Feb 21 '13 at 4:09
Mark Rajcok
353k113113 gold badges486486 silver badges487487 bronze badges
Something similar to Unix's timestamp, that is a single number that represents the current time and date. Either as a number or a string.
asked Oct 21 '08 at 9:29
pupeno
264k117117 gold badges337337 silver badges558558 bronze badges
How do I make the first letter of a string uppercase, but not change the case of any of the other letters?
For example:
"this is a test" → "This is a test"
"the Eiffel Tower&...
asked Jun 22 '09 at 8:25
Robert Wills
44.8k33 gold badges1616 silver badges66 bronze badges
I'd like to do something like this to tick a checkbox using jQuery:
$(".myCheckBox").checked(true);
or
$(".myCheckBox").selected(true);
Does such a thing exist?
asked Jan 8 '09 at 22:20
tpower
54.1k1818 gold badges6666 silver badges100100 bronze badges
Why does Google prepend while(1); to their (private) JSON responses?
For example, here's a response while turning a calendar on and off in Google Calendar:
while (1);
[
['u', [
['smsSentFlag', ...
asked Apr 19 '10 at 18:00
Jess
40.5k66 gold badges3535 silver badges5151 bronze badges
The following are two methods of building a link that has the sole purpose of running JavaScript code. Which is better, in terms of functionality, page load speed, validation purposes, etc.?
...
What is the best way to copy text to the clipboard (multi-browser)?
I have tried:
function copyToClipboard(text) {
if (window.clipboardData) { // Internet Explorer
window.clipboardData....
asked Dec 30 '08 at 13:09
After an AJAX request, sometimes my application may return an empty object, like:
var a = {};
How can I check whether that's the case?
asked Mar 25 '09 at 1:39
falmp
39.4k33 gold badges1919 silver badges1818 bronze badges
I saw some code that seems to use an operator I don't recognize, in the form of two exclamation points, like so: !!. Can someone please tell me what this operator does?
The context in which I saw ...
asked Apr 24 '09 at 8:13
Hexagon Theory
39.4k55 gold badges2424 silver badges3030 bronze badges
In Java you can use a for loop to traverse objects in an array as follows:
String[] myStringArray = {"Hello", "World"};
for (String s : myStringArray)
{
// Do something
}
Can you do the same in ...
asked Jun 10 '10 at 0:04
Mark Szymanski
52.2k2323 gold badges6767 silver badges8787 bronze badges
I am looking for a JavaScript array insert method, in the style of:
arr.insert(index, item)
Preferably in jQuery, but any JavaScript implementation will do at this point.
asked Feb 25 '09 at 14:29
tags2k
74.1k3030 gold badges7575 silver badges105105 bronze badges
How do I check if a particular key exists in a JavaScript object or array?
If a key doesn't exist, and I try to access it, will it return false? Or throw an error?
asked Jul 8 '09 at 13:21
Adam Ernst
47.7k1818 gold badges5656 silver badges7171 bronze badges
I have an array of JavaScript objects:
var objs = [
{ first_nom: 'Lazslo', last_nom: 'Jamf' },
{ first_nom: 'Pig', last_nom: 'Bodine' },
{ first_nom: 'Pirate', last_nom: '...
I saw this question, but I didn't see a JavaScript specific example. Is there a simple string.Empty available in JavaScript, or is it just a case of checking for ""?
asked Sep 30 '08 at 17:17
casademora
62.3k1515 gold badges6767 silver badges7878 bronze badges
I'd like to round at most 2 decimal places, but only if necessary.
Input:
10
1.7777777
9.1
Output:
10
1.78
9.1
How can I do this in JavaScript?
I have an object x. I'd like to copy it as object y, such that changes to y do not modify x. I realized that copying objects derived from built-in JavaScript objects will result in extra, unwanted ...
asked Apr 8 '09 at 3:01
soundly_typed
36.1k55 gold badges2525 silver badges3333 bronze badges
All I want is to get the website URL. Not the URL as taken from a link. On the page loading I need to be able to grab the full, current URL of the website and set it as a variable to do with as I ...
asked Jun 23 '09 at 19:26
What is the difference between using Function.prototype.apply() and Function.prototype.call() to invoke a function?
var func = function() {
alert('hello!');
};
func.apply(); vs func.call();
Are ...
asked Dec 31 '09 at 19:56
John Duff
36.4k44 gold badges3232 silver badges4545 bronze badges
I have a JavaScript object like the following:
var p = {
"p1": "value1",
"p2": "value2",
"p3": "value3"
};
Now I want to loop through all p elements (p1, p2, p3...) And get their keys ...
asked Mar 26 '09 at 6:01
Tanmoy
40.8k1414 gold badges4343 silver badges5454 bronze badges
I'm trying to write a function that either accepts a list of strings, or a single string. If it's a string, then I want to convert it to an array with just the one item so I can loop over it without ...
asked Jan 23 '11 at 18:53
mpen
248k238238 gold badges784784 silver badges11481148 bronze badges
When I want to prevent other event handlers from executing after a certain event is fired, I can use one of two techniques. I'll use jQuery in the examples, but this applies to plain-JS as well:
1. ...
asked Aug 31 '09 at 11:58
RaYell
67.2k2020 gold badges124124 silver badges149149 bronze badges
How can I change the class of an HTML element in response to an onclick or any other events using JavaScript?
asked Oct 12 '08 at 20:06
Nathan Smith
34.1k66 gold badges2626 silver badges2525 bronze badges
Can I convert a string representing a boolean value (e.g., 'true', 'false') into a intrinsic type in JavaScript?
I have a hidden form in HTML that is updated based upon a user's selection within a ...
What's the best way of checking if an object property in JavaScript is undefined?
asked Aug 26 '08 at 7:25
Matt Sheppard
112k4646 gold badges107107 silver badges128128 bronze badges
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);
...
asked Apr 15 '09 at 6:06
nickf
510k196196 gold badges624624 silver badges711711 bronze badges
I would like to upload a file asynchronously with jQuery.
$(document).ready(function () {
$("#uploadbutton").click(function () {
var filename = $("#file").val();
$.ajax({...
asked Oct 3 '08 at 10:22
Sergio del Amo
73.1k6666 gold badges149149 silver badges178178 bronze badges
I have the following code in Ruby. I want to convert this code into JavaScript. What is the equivalent code in JS?
text = <<"HERE"
This
Is
A
Multiline
String
HERE
asked Apr 30 '09 at 2:11
Newy
35.3k99 gold badges4040 silver badges5555 bronze badges
What I am looking for:
A way to style one HALF of a character. (In this case, half the letter being transparent)
What I have currently searched for and tried (With no luck):
Methods for styling ...
asked May 9 '14 at 16:16
Mathew MacLean
23.7k33 gold badges2121 silver badges3939 bronze badges
Is there a better way to engineer a sleep in JavaScript than the following pausecomp function (taken from here)?
function pausecomp(millis)
{
var date = new Date();
var curDate = null;
do ...
How can I check the existence of an element in jQuery?
The current code that I have is this:
if ($(selector).length > 0) {
// Do something
}
Is there a more elegant way to approach this? ...
asked Aug 27 '08 at 19:49
Jake McGraw
53.5k1010 gold badges4747 silver badges6363 bronze badges
How can I display JSON in an easy-to-read (for human readers) format? I'm looking primarily for indentation and whitespace, with perhaps even colors / font-styles / etc.
asked Jan 26 '11 at 22:33
Mark
60.3k4545 gold badges114114 silver badges154154 bronze badges