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 76 of 801
What is onclick not working for two or more links for same function in JavaScript?
If onclick not working for two or more links, then use the document.querySelectorAll() in JavaScript. Following is the code −Example Document www.example.com www.test.com const values = document.querySelectorAll('[class=class1]'); values.forEach(function(v) { console.log(v.innerHTML); }); 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 MoreChange value of input on form submit in JavaScript?
To change value of input, use the concept of document. The syntax is as follows −document.yourFormName.yourTextName.value=anyValue;Let’s say the following is our input type calling submitForm() on clicking “Submit Form” − The submitForm() function changing the value of input −function submitForm(){ document.studentForm.txtInput.value='STUDENT-100'; }Example Document function submitForm(){ document.studentForm.txtInput.value='STUDENT-100'; } 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 ...
Read MoreHow to remove li elements on button click in JavaScript?
Let’s say the following is our Unordered List (ul) − JavaScript Remove MySQL Remove MongoDB Remove Java Remove Above, you can see the “Remove” button with every li element. On clicking this button, you can remove any li element.Following is the code to remove li elements on button click;Example Document Remove the subjects The Subjects are as follows: JavaScript Remove MySQL Remove MongoDB Remove Java Remove var allSubjectName = document.querySelectorAll(".subjectName"); for (var index = 0; index
Read MoreReplace HTML div into text element with JavaScript?
For this, use document.querySelectorAll(). With that, also use the getElementsByClassName(). Following is the code −Example Document My Name is John My h6 value must be here... My h6 value must be here... My h6 value must be here... const allSpan = document.querySelectorAll('.my-main-div-class span'), repaceAboveText = document.getElementsByClassName('uniqueId')[0].textContent; for (var newElement of allSpan){ newElement.textContent=repaceAboveText } 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 MoreIs there a DOM function which deletes all elements between two elements in JavaScript?
Let’s say the following are our elements −My Name is John My Name is David My Name is Bob My Name is Mike My Name is Carol ENDWe need to remove theelement and its content. Theelements are in between the START and END.To remove elements between two elements, use the concept of remove(). Following is the code −Example Document START My Name is John My Name is David My Name is Bob My Name is Mike My Name is Carol END const startingPoint = document.querySelector("nav"); const endingPoint = document.querySelector("footer"); while ...
Read MoreHow to display only the current year in JavaScript?
To display only the current year, use getFullYear(). Following is the code −Example Document The Current Year= document.getElementById("theCurrentYear").innerHTML = new Date().getFullYear(); 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 MoreDisplay whether the office is closed or open right now on the basis of current time with JavaScript ternary operator
Let’s say, we are matching the current date and time with the business hours. We need to display whether the office is closed or open right now on the basis of current time.Get the hours from the current date and can use the ternary operator for close and open. Following is the code −Example Document const gettingHours = new Date().getHours() const actualHours = (gettingHours >= 10 && gettingHours < 18) ? 'Open' : 'Closed'; document.querySelector('.closeOrOpened').innerHTML = actualHours; To run the above program, save the ...
Read MoreHow to display JavaScript array items one at a time in reverse on button click?
Let’s say the following is our array −var listOfNames = [ 'John', 'David', 'Bob' ];Following is our button −Click The Button To get the Reverse ValueNow, for array items in reverse, at first, reach the array length and decrement the length by 1. Then, print the contents of the particular index in reverse order.Example Document Click The Button To get the Reverse Value var listOfNames = [ 'John', 'David', 'Bob' ]; count=listOfNames.length-1; function reverseTheArray(){ document.getElementById('reverseTheArray').innerHTML = listOfNames[count]; count--; if (count
Read MoreHTML form action and onsubmit validations with JavaScript?
Let’s see an example wherein we are validating input text onsubmit −Example Document function validateTheForm(){ var validation = (document.getElementById('txtInput').value == 'gmail'); if(!validation){ alert('Something went wrong...Plese write gmail intext box and click'); return false; } return true; } 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 ...
Read MoreHow to convert comma separated text in div into separate lines with JavaScript?
Let’s say the following is our comma separated text in div − This, is, the, first, JavaScript, program To convert comma separated text into separate lines, you need to use trim() along with split() on the basis of comma(, ).Example Document This, is, the, first, JavaScript, program var allTheData = document.querySelector('.commaSeparated').textContent.trim().split(', ') var separateList = '' allTheData.forEach(function(value) { separateList += '' + value + ''; }); separateList += ''; document.querySelector(".commaSeparated").innerHTML = separateList; To run the above ...
Read More