Found 6710 Articles for Javascript

How to make a setInterval() stop after some time or after a number of actions in JavaScript?

AmitDiwan
Updated on 26-Oct-2020 11:06:41

2K+ Views

Use some conditions to stop after some time.The below code will stop in half a minute.ExampleFollowing is the code −            Document    var now = new Date().getTime();    var interval = setInterval(function () {       if (new Date().getTime() - now > 30000) {          clearInterval(interval);          return;       }       console.log("working");    }, 2000); To run the above program, save the file name anyName.html(index.html) and right click on the file. Select the option “Open with live server” in VS Code editor.OutputThis will produce the following output −

How to sort an array of integers correctly in JavaScript?

AmitDiwan
Updated on 26-Oct-2020 11:04:21

380 Views

To sort an array of integers, use sort() in JavaScript.ExampleFollowing is the code −var arrayOfIntegers = [67, 45, 98, 52]; arrayOfIntegers.sort(function (first, second) {    return first - second; }); console.log(arrayOfIntegers);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo310.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo310.js [ 45, 52, 67, 98 ]

How to stop the loop for JavaScript scroll down?

Yaswanth Varma
Updated on 18-Jan-2023 11:17:48

777 Views

JavaScript scroll down loops can be extremely annoying and disruptive to a web page. Fortunately, it is relatively easy to stop the loop if you know what steps to take. In this article, we will discuss how to stop the JavaScript scroll down loop so that your website visitors don't have to keep scrolling endlessly. We'll also provide some tips on preventing these types of issues in the future. JavaScript method ScrollBy() scrolls the web page to the specific number of pixels. The clearInterval()in JavaScript The clearInterval() is a JavaScript function that is used to stop the execution of a ... Read More

JavaScript subtraction of two float values?

Yaswanth Varma
Updated on 18-Jan-2023 11:44:20

3K+ Views

As we are familiar with the subtraction of two float values and we know how to use the subtraction condition between float values. To correctly subtract two float values, use parseFloat() along with toFixed(). Let’s dive into the article to learn more about JavaScript subtraction of two float values. The parseFloat()function The string is taken as input and converted into a floating-point value using the parseFloat() method. The function returns NaN, or not a number, if the string is empty or if its first character is not a numeric value. Up until the point where it runs into ... Read More

Use jQuery to get values of selected checkboxes?

AmitDiwan
Updated on 26-Oct-2020 10:56:01

436 Views

Yes, we can use jQuery to get values of selected checkboxes using the concept of input. We have to also use the :checked selector.ExampleFollowing is the code −            Document        Cricket        Listening Music Reading NewsPaper Click Me    $(document).ready(function () {       $("button").click(function () {          $('input[name="checkDemo"]:checked').each(function () {             console.log(this.value);          });       });    }); To run the above program, save the ... Read More

Replace whitespace in different elements with the same class in JavaScript?

Yaswanth Varma
Updated on 18-Jan-2023 12:04:10

449 Views

In this article we are going to learn about replace whitespace in different elements with same class in JavaScript. Let’s look into the article to learn more about replacing whitespaces in different elements. Using split() and join() Methods The split() method divides a string into several substrings and returns them as an array. When a separator is present in the string, the string is split according to the separator that was specified as a parameter. This parameter specifies the space character (" ") to divide the string whenever a space is present. A separator is used to join an array ... Read More

How can I show a hidden div when a select option is selected in JavaScript?

Yaswanth Varma
Updated on 18-Jan-2023 12:06:21

17K+ Views

Showing and hiding elements is a common task in JavaScript. It can be used to create dynamic user interfaces, where the content changes depending on the user's input or interaction with the page. One way of doing this is to show a hidden div when a select option is selected. This article will guide you through how to do this using JavaScript. A Boolean attribute is the one that was chosen. It indicates that a choice should be preselected when the page loads if it is present. In the drop-down list, the pre-selected option will be shown first. To ... Read More

Why addEventListener to 'select' element does not work in JavaScript?

Yaswanth Varma
Updated on 18-Jan-2023 12:02:43

9K+ Views

When a user changes the value of an element, the , , and elements fire the change event. The change event does not always fire for every change to an element's value, unlike the input event. Instead, use querySelector() along with addEventListener(). Let’s discuss one by one to understand how they will work. The addEventListener() Method − The addEventListener() is a method used to register an event handler for the specified event type on the current element. It allows you to specify a function that will be called whenever the specified event occurs on the element. Following ... Read More

How to dynamically create radio buttons using an array in JavaScript?

Yaswanth Varma
Updated on 18-Jan-2023 12:07:43

5K+ Views

Creating radio buttons dynamically using an array in JavaScript is a useful way to provide users with multiple options for input. When the user clicks on one of the radio buttons, it will be selected and any other choices will be deselected automatically. This process can be automated by looping through an array that contains all the possible values and creating a new radio button element for each item in the array. To dynamically create radio button using an array, use the concept of createElement() and appendChild().Let’s look at the concept to understand more about dynamically creating a radio buttons. ... Read More

WebDriver click() vs JavaScript click().

Debomita Bhattacharjee
Updated on 26-Oct-2020 06:53:23

748 Views

We can click a link with the webdriver click and Javascript click. For the Selenium webdriver click of a link we can use link text and partial link text locator. We can use the methods driver.findElement(By.linkText()) and driver.findElement(By.partialLinkText()) to click.The links in an html code are enclosed in an anchor tag. The link text enclosed within the anchor tag is passed as argument to the driver.findElement(By.linkText()) method. The partial matching link text enclosed within the anchor tag is passed as argument to the driver.findElement(By.partialLinkText()) method. Finally to click on the link the click method is used.Let us see the html ... Read More

Advertisements