Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
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?
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 MoreEnter values with prompt and evaluate on the basis of conditions in JavaScript?
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 MoreHow to get value of data attribute and use it in jQuery?
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 MoreMatch the style of <pre> element while performing paste - jQuery?
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 MoreChecking a Checkbox with JavaScript
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 MoreDifference between two times using Dayjs JavaScript library?
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 MoreDisplay array items on a div element on click of button using vanilla JavaScript
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 MoreJavaScript get the length of select options(dropdown)?
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 MoreJavaScript Sum function on the click of a button
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 MoreaddEventListener() not working more than once with a button in JavaScript?
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