Web Development Articles

Page 72 of 801

How do you remove all the options of a select box and then add one option and select it with jQuery?

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 771 Views

To remove, use remove() and append() to add option in already created select list. Let’s say the following is our select −    JavaScript    MySQL ExampleFollowing is the code to remove all the options and add a new one −            Document           JavaScript       MySQL        $('#demo')    .find('option')    .remove()    .end()    .append('MongoDB')    .val('MongoDB')    ; 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.OutputThe output is as follows −

Read More

Enter values with prompt and evaluate on the basis of conditions in JavaScript?

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 279 Views

Let’s say we are getting two values with prompt −var firstvalue = parseInt(prompt("Enter the value1")); var secondvalue = parseInt(prompt("Enter the value2"));ExampleFollowing is the code −            Document    var firstvalue = parseInt(prompt("Enter the value1"));    var secondvalue = parseInt(prompt("Enter the value2"));    if (firstvalue * secondvalue > 50)       document.write("Result is correct");    else       document.write("Result is not correct"); To run the above program, save the file name anyName.html (index.html). Right click on the file and select the option “Open with ...

Read More

How to get value of data attribute and use it in jQuery?

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

To get value of data attribute, use −$(“yourSelector”).data()The following is our input type with data attribute −ExampleFollowing is the code −            Document        var value = $('#txtValue').data('value');    console.log("The value is=" + value); OutputThe output is as follows −

Read More

Match the style of <pre> element while performing paste - jQuery?

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 144 Views

For this, use tag. Set it to contenteditable −We have set the following style for paste −    pre {       min-height: 150px;       min-width: 300px;       font-family: 'Times New Roman', Times, serif;       white-space: pre;       background-color: rgb(19, 22, 27);       color: #98d8e7;    } Now, you can use paste event listener −var getTheData = document.getElementById('data'); getTheData.addEventListener('paste', PutTheDataOnEditor);ExampleFollowing is the code −            Document pre {    min-height: 150px;    min-width: 300px;    font-family: 'Times ...

Read More

Checking a Checkbox with JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 589 Views

Let’s say the following are ourrinput type checkbox −John David We want to check any of the checkbox. Use the checked property to check the checkbox.ExampleFollowing is the code −            Document    John        David        document.querySelector('#checkedValue2').checked = true 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 −OutputThe output is as follows −

Read More

Difference between two times using Dayjs JavaScript library?

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 533 Views

Let’s say the following are our time data −var startHour = dayjs().hour(10) var endHour = dayjs().hour(22)To get the difference, use the diff() method −ExampleFollowing is the code −            Document    var startHour = dayjs().hour(10)    var endHour = dayjs().hour(22)    console.log("The hours difference is=" + endHour.diff(startHour, "hours")); 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 −OutputThe output is as follows −

Read More

Display array items on a div element on click of button using vanilla JavaScript

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

To embed the elements of an array inside a div, we just need to iterate over the array and keep appending the element to the divThis can be done like this −Exampleconst myArray = ["stone", "paper", "scissors"]; const embedElements = () => {    myArray.forEach(element => {       document.getElementById('result').innerHTML +=       `${element}`;       // here result is the id of the div present in the DOM    }); };This code makes the assumption that the div in which we want to display the elements of array has an id ‘result’.The complete code for this ...

Read More

JavaScript get the length of select options(dropdown)?

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

Let’s say the following is our dropdown (select) −    John    Mike    Sam    David    Carol Following is the code to get the length of list options in JavaScript −Example Document John Mike Sam David Carol    var lengthOfListOptions = $("#lengthListOfOptionDemo option").length;    console.log("The length is=" + lengthOfListOptions); 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

JavaScript Sum function on the click of a button

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

Let’s say the following is our button −Sum We are calling the function addTheValue(10) with parameter 10 on button click. On clicking the button, we are adding the value 10 as in the below code −Example Document Adding 10 each time whenever you click the Sum Button...... 10 Sum    function addTheValue(secondValue) {       var fValue = document.getElementById("firstValue");       firstValue.innerHTML = parseInt(fValue.innerHTML) +       parseInt(secondValue);    } To run the above program, save the file name ...

Read More

addEventListener() not working more than once with a button in JavaScript?

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

To make addEventListener() repeatedly work on button clicks, use the following code −Example Document Click Me    var eventValue = function (event) {       document.body.appendChild(document.createElement('div'))       .textContent = event.type;    }    var pressed = document.querySelector("#pressButtonDemo");    pressed.addEventListener("click", eventValue); 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 −When you click the button “Click Me” then you will get the ...

Read More
Showing 711–720 of 8,006 articles
« Prev 1 70 71 72 73 74 801 Next »
Advertisements