Questions tagged [javascript]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [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
108k2323 gold badges6565 silver badges9393 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
122k3636 gold badges8484 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
91k1818 gold badges8080 silver badges130130 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
88.4k1212 gold badges5151 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
718k163163 gold badges10151015 silver badges10811081 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
86.4k3535 gold badges115115 silver badges179179 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.
97k3030 gold badges120120 silver badges127127 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(...
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
53k1212 gold badges3434 silver badges5353 bronze badges
Is there a regular expression to verify an email address in JavaScript?
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
239k251251 gold badges556556 silver badges738738 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, ...
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
351k113113 gold badges484484 silver badges483483 bronze badges
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
68.6k2121 gold badges6969 silver badges8484 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
259k116116 gold badges328328 silver badges548548 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
53.5k1818 gold badges6565 silver badges9999 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
43.4k33 gold badges1616 silver badges66 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.1k66 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.?
...
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
38.9k33 gold badges1919 silver badges1818 bronze badges
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
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
38.3k55 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
51.1k2222 gold badges6666 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
72.3k3030 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
46.6k1818 gold badges5656 silver badges7171 bronze badges
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
35.5k44 gold badges2525 silver badges3333 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
61.1k1515 gold badges6666 silver badges7878 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
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?
What is the difference between using call and apply to invoke a function?
var func = function() {
alert('hello!');
};
func.apply(); vs func.call();
Are there performance differences between the ...
asked Dec 31 '09 at 19:56
John Duff
36k44 gold badges3131 silver badges4444 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.1k1414 gold badges4242 silver badges5353 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
66.7k2020 gold badges124124 silver badges149149 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
242k235235 gold badges777777 silver badges11321132 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
72.4k6666 gold badges148148 silver badges177177 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
33.4k66 gold badges2525 silver badges2525 bronze badges
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
504k196196 gold badges617617 silver badges709709 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
34.7k99 gold badges4040 silver badges5555 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 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.4k33 gold badges2020 silver badges3939 bronze badges
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
53k1010 gold badges4747 silver badges6363 bronze badges
How do I append an object (such as a string or number) to an array in JavaScript?
I have two radio buttons and want to post the value of the selected one.
How can I get the value with jQuery?
I can get all of them like this:
$("form :radio")
How do I know which one is selected?
asked Feb 27 '09 at 19:53
juan
74.9k4949 gold badges156156 silver badges189189 bronze badges