Javascript Articles

Page 52 of 534

Set the value in local storage and fetch – JavaScript?

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 639 Views

Use localStorage.setItem(“anyKeyName”, yourValue) to set the value in local storage and if you want to fetch value then you can use localStorage.getItem(“yourKeyName”)ExampleFollowing is the code −            Document        var textBoxNameHTML = document.getElementById('txtName');    textBoxNameHTML.addEventListener('change', (e) => {       localStorage.setItem("textBoxValue", e.target.value);    }); To run the above program, save the file name “anyName.html(index.html)”. Right click on the file and select the option “Open with Live Server” in VS Code editor.OutputThis will produce the following output on console −Now, I am ...

Read More

How to make a condition for event 'click' inside/outside for multiple divs - JavaScript?

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 1K+ Views

You can use event listeners for clicks.ExampleFollowing is the code −            Document           First Division               Second Division      document.addEventListener('click', callEventFuncion)    function callEventFuncion(event) {       var div = document.querySelectorAll('.divDemo');       var titleResult = document.querySelectorAll('.my-title');       var result = Array.apply(0, div).find((v) => v.contains(event.target));       if (result) {          console.log(" Incrementing Division Selection");       }       else {   ...

Read More

Make JavaScript take HTML input from user, parse and display?

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 2K+ Views

The HTML input value is a string. To convert the string to integer, use parseInt().ExampleFollowing is the code −            Document        GetANumber    function result() {       var numberValue = document.getElementById("txtInput").value;       if (!isNaN(numberValue))          console.log("The value=" + parseInt(numberValue));       else          console.log("Please enter the integer value..");    } To run the above program, save the file name “anyName.html(index.html)”. Right click on the file and select the option “Open with Live ...

Read More

How to change the color of a button when the input field is filled – JavaScript?

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 2K+ Views

Let’s say the following is our button −Press MeOn filling the below input field, the color of the above button should change −ExampleFollowing is the code −            Document           UserName:                Press Me    function changeTheColorOfButtonDemo() {       if (document.getElementById("changeColorDemo").value !== "") {          document.getElementById("buttonDemo").style.background = "green";       } else {          document.getElementById("buttonDemo").style.background = "skyblue";       }    } To run the ...

Read More

How to get id from tr tag and display it in a new td with JavaScript?

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 4K+ Views

Let’s say the following is our table −           StudentName       StudentCountryName               JohnDoe       UK               DavidMiller       US     To get id from tr tag and display it in a new td, use document.querySelectorAll(table tr).ExampleFollowing is the code −            Document    td,    th,    table {       border: 1px solid black;       margin-left: 10px;       margin-top: ...

Read More

How to transform two or more spaces in a string in only one space? JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 338 Views

We have to write a JavaScript program that takes a variable user string through an input in HTML. Then through JavaScript the program should check for more than one consecutive spaces in the string.And the program should replace all such instances of more than one consecutive spaces with only one space.We can use a regular expression as the first parameter of replace. /\s{2, }/g to achieve the desired results. Let us write the code for this function −Example REMOVE SPACES    function removeSpaces() {       var textInput = insertText.value;     ...

Read More

How to set text in <strong> tag with JavaScript?

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 1K+ Views

At first, set the element −Replace This strong tagThe id attribute set above would be used set the text using # −$(document).ready(function(){    $("#strongDemo").html("Actual value of 5+10 is 15....."); });Example Document Replace This strong tag    $(document).ready(function(){       $("#strongDemo").html("Actual value of 5+10 is 15.....");    }); 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 −

Read More

How do I display the content of a JavaScript object in a string format?

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 316 Views

Use document.getElementById() and display using innerHTML. Following is the code −Example Document    var jsonObject="name";    var value = jsonObject.split(" ");    var add = {};    value.forEach(function(v){       add[v] ? add[v]++ : add[v] = "John";    });    document.getElementById("objectId").innerHTML = JSON.stringify(add); 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 −

Read More

Attach event to dynamic elements in JavaScript?

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 233 Views

To attach event, use document.addEventListener() in JavaScript. Following is the code −Example Document    document.addEventListener('click',function(event){       if(event.target && event.target.id== 'buttonSubmit'){          console.log("Event generation on the button click")       }    }); 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 −After clicking the submit button, the screenshot is as follows −

Read More

How to detect the screen resolution with JavaScript?

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 1K+ Views

To detect the screen resolution, use the concept of window.screen.For width, use the following −window.screen.availWidthFor height −window.screen.availHeightFollowing is the code to detect the screen resolution −Example Document    console.log("Available Width="+window.screen.availWidth);    console.log("Available Height="+window.screen.availHeight); 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 −

Read More
Showing 511–520 of 5,338 articles
« Prev 1 50 51 52 53 54 534 Next »
Advertisements