Found 6683 Articles for Javascript

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

16K+ 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

7K+ 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

522 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

Reading JavaScript variables using Selenium WebDriver.

Debomita Bhattacharjee
Updated on 26-Oct-2020 06:27:47

2K+ Views

We can read Javascript variables with Selenium webdriver. Selenium can run Javascript commands with the help of executeScript method. The Javascript command to be executed is passed as an argument to the method. Also we have to add the statement import org.openqa.selenium.JavascriptExecutor to work with Javascript.SyntaxJavascriptExecutor j = (JavascriptExecutor) driver; j.executeScript("return document.title")Let us obtain the browser title of the below page by reading the value from Javascript variable. The output should be About Careers at Tutorials Point – Tutorialspoint.ExampleCode Implementationimport org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import org.openqa.selenium.JavascriptExecutor; public class JavascriptReadValue{    public static void main(String[] args) {   ... Read More

Running javascript in Selenium using Python.

Debomita Bhattacharjee
Updated on 26-Oct-2020 06:16:07

622 Views

We can run Javascript in Selenium webdriver with Python. The Document Object Model communicates with the elements on the page with the help of Javascript. Selenium executes the Javascript commands by taking the help of the execute_script method. The commands to be executed are passed as arguments to the method.Some operations like scrolling down in a page cannot be performed by Selenium methods directly. This is achieved with the help of Javascript Executor. The window.scrollTo method is used to perform scrolling operation. The pixels to be scrolled horizontally along x axis and pixels to be scrolled vertically along y axis ... Read More

Getting the return value of Javascript code in Selenium.

Debomita Bhattacharjee
Updated on 26-Oct-2020 06:04:17

3K+ Views

We can get the return value of Javascript code with Selenium webdriver. Selenium can run Javascript commands with the help of executeScript method. The Javascript command to be executed is passed as an argument to the method.We shall be returning the value from the Javascript code with the help of the keyword return. Also we have to add the statement import org.openqa.selenium.JavascriptExecutor to work with Javascript.SyntaxJavascriptExecutor j = (JavascriptExecutor) driver; j.executeScript("return document.getElementsByName('txtSearchText')[0].value")Let us obtain the value entered in the edit box. The output should be Selenium.ExampleCode Implementationimport org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import org.openqa.selenium.JavascriptExecutor; public class ... Read More

Match specific word in regex in JavaScript?

Nikhilesh Aleti
Updated on 22-Sep-2022 12:05:51

10K+ Views

The task is to match a specific word or character which is in regex with a string. The regex (regular expressions) is a pattern that is used to match character combinations in strings. Here we include the test(), match(), and matchAll() methods to match the following word in regex. We have some boundary-type assertions, in which we have used \b. Consider a sentence – “mickey is holding mic.” Using regex - \bmic\b will match the word mic but not the word mic in mickey. It is a word boundary. Another assertion is (g), It is a global search flag. Consider ... Read More

Get price value from span tag and append it inside a div after multiplying with a number in JavaScript?

AmitDiwan
Updated on 24-Oct-2020 12:33:33

1K+ Views

Extract the value and convert it from String to integer to get price value from span tag.ExampleFollowing is the code −            Document           Value=                10                    $(document).ready(function () {       var v = parseInt($(".purchase.amount")       .text()       .trim()       .replace(', ', ''));       var totalValue = v * 10;       console.log(totalValue);   ... Read More

Removing listener from inside outer function in JavaScript?

AmitDiwan
Updated on 24-Oct-2020 12:28:41

60 Views

To remove listener from outer function, use removeEventListener().ExampleFollowing is the code − Document Press Me    var demoId = document.getElementById('demo');    demoId.addEventListener('click', function fun() {       outerFunction(this, fun);    }, false);    function outerFunction(self, funct) {       console.log('outer function is called....');       self.removeEventListener('click', funct, false);       console.log("Listener has been removed...")    } 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 ... Read More

Advertisements