Pass Arguments to Anonymous Functions in JavaScript

AmitDiwan
Updated on 18-Jul-2020 07:19:39

1K+ Views

Following is the code for passing arguments to anonymous functions in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-weight: 500;       font-size: 20px;       color: blueviolet;    } Pass arguments to anonymous functions CLICK HERE Click on the above button to call an anonymous function and pass parameters to it    let BtnEle = document.querySelector(".Btn");    let resEle = document.querySelector(".result");    let add = function (a, b) {       return a + b;    };    BtnEle.addEventListener("click", (event) => {       resEle.innerHTML = "The sum of 22 and 19 = " + add(22, 19);    }); OutputOn clicking the ‘CLICK HERE’ button −

Get Child Element of a Parent Using JavaScript

AmitDiwan
Updated on 18-Jul-2020 07:19:35

576 Views

Following is the code for getting the child element of a parent using JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .child1,    .child2,    .child3 {       display: none;       margin: 10px;       width: 70px;       height: 70px;    }    .child1 {       background-color: red;    }    .child2 {       background-color: pink;    }    .child3 {       background-color: blue;    } Get ... Read More

Uses of the JavaScript with Statement

AmitDiwan
Updated on 18-Jul-2020 07:17:43

1K+ Views

The WITH statement is used to specify the default object for the given property and allow us to prevent writing long lengthy object references. It adds the given object to the head of the scope chain.Following is the code for with statement in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 20px;       font-weight: 500;    } JavaScript with statement CLICK HERE Click on the above button to get the first ... Read More

Accessing a Parent Element Using JavaScript

AmitDiwan
Updated on 18-Jul-2020 07:16:54

503 Views

Following is the code for accessing a parent element using JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 18px;       font-weight: 500;       color: #8a2be2;    }    .parent1,    .parent2,    .parent3 {       display: inline-block;       margin: 10px;    }    .child1,    .child2,    .child3 {       width: 70px;       height: 70px;    }    .child1 {       background-color: red; ... Read More

Check Whether a Button is Clicked with JavaScript

AmitDiwan
Updated on 18-Jul-2020 07:14:56

1K+ Views

Following is the code for checking whether a button is clicked using JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 18px;       font-weight: 500;       color: blueviolet;    } Checking whether a Button is clicked using JavaScript CLICK HERE Click on the above button to check if the button is clicked    let BtnEle = document.querySelector(".Btn");    let resEle = document.querySelector(".result");    let clickCount = 0;    BtnEle.addEventListener("click", () => {       clickCount++;       resEle.innerHTML =       "The button has been clicked " + clickCount + " times ";    }); OutputOn clicking the button 5 times −

Alert for Unsaved Changes in Form using JavaScript

AmitDiwan
Updated on 18-Jul-2020 07:13:19

716 Views

Following is the code to display an alert for form’s unsaved changes in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    input {       margin: 10px;    } Alert for unsaved changes in form Username Password SUBMIT    function pageUnload() {       return "The data on this page will be lost if you leave";    } OutputIf we click on the reload button then the following warning will be shown −

Find Coordinates of Every Link in a Page using JavaScript

AmitDiwan
Updated on 18-Jul-2020 07:12:52

196 Views

Following is the code to find the coordinates of every link in a page using JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 20px;       font-weight: 500; } Coordinates of every link in a page google.com facebook.com yahoo.com CLICK HERE Click on the above button to display the link coordinates    let resEle = document.querySelector(".result");    let BtnEle = document.querySelector(".Btn");    let links = document.getElementsByTagName("a");    BtnEle.addEventListener("click", () ... Read More

Circle Coordinates to Array in JavaScript

AmitDiwan
Updated on 18-Jul-2020 07:11:39

475 Views

Following is the code to circle coordinates to array in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-weight: 500;       font-size: 18px;       color: blueviolet;    } Circle coordinates to array CLICK HERE Click on the above button to generate the circle coordinates    let resEle = document.querySelector(".result");    let BtnEle = document.querySelector(".Btn");    let xCoords = [];    let yCoords = [];    function circleCoordinates(radius, steps, ... Read More

Retrieve Text Contents of User Selection Using JavaScript

AmitDiwan
Updated on 18-Jul-2020 07:08:57

132 Views

Following is the code to retrieve the text contents of the user selection using JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result, .sample {       font-size: 20px;       font-weight: 500;       color: rebeccapurple;    } Retrieve the text contents of the user selection Here is some text inside the div SELECT Click on the above button to display the text selected by the user    let resEle = document.querySelector(".result"); ... Read More

Select and Deselect Text Inside an Element Using JavaScript

AmitDiwan
Updated on 18-Jul-2020 07:06:25

3K+ Views

Following is the code to select and deselect text inside an element using JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 20px;       font-weight: 500;    } Select and Deselect Text Inside an Element Here is some text inside the div SELECT DESELECT Click on the above button to select/de-select text inside the div    let resEle = document.querySelector(".result");    let BtnEle = document.querySelectorAll(".Btn");    BtnEle[0].addEventListener("click", () => {       window.getSelection().selectAllChildren(resEle);    });    BtnEle[1].addEventListener("click", () => {       window.getSelection().removeAllRanges();    }); OutputThe above code will produce the following output −On clicking the ‘SELECT’ button −On clicking the ‘DESELECT’ button −

Advertisements