- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 9313 Articles for Object Oriented Programming
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
616 Views
To extract the value of the , use −document.getElementById(“yourTextIdValue”).textContentExample Live Demo Document This is the JavaScript.... var originalText = document.getElementById("myText").textContent; console.log(originalText); To run the above program, just save the file name anyName.html(index.html) and right click on the file and select the option open with live server in VS Code editor.OutputThis will produce the following output display the value −
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
5K+ Views
In order to set object values to null, you need to get all the keys from the object and need to set null. You can use for loop or forEach() loop.Following is the JavaScript code with name records. We will set the object value to null −const object=[ { FirstName:"John" }, { FirstName:"David" }, { FirstName:"Bob" } ] Object.keys(object).forEach(key => object[key]=null); console.log(object);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo31.js.OutputThis will produce the following output ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
590 Views
Let’s say we have the following strings. One of them is beginning with a question mark i.e. punctuation −var sentence1 = 'My Name is John Smith.' var sentence2 = '? My Name is John Smith.'We need to check whether any of the above two sentences begin with punctuation. To check if string begins with punctuation, the code is as follows −Examplevar punctuationDetailsRegularExpression=/^[., :!?]/ var sentence1 = 'My Name is John Smith.' var output1 = !!sentence1.match(punctuationDetailsRegularExpression) if(output1==true) console.log("This ("+sentence1+") starts with a punctuation"); else console.log("This ("+sentence1+") does not starts with a punctuation"); var sentence2 = '? My Name is ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
140 Views
Let’s say the following is our string with whitespace −var fullName=" John Smith ";Use replace() and set Regex in it to remove whitespaces.Examplefunction removeSpacesAtTheBeginningAndTheEnd(name) { return name.toString().replace(/^\s+|\s+$/g,''); } var fullName=" John Smith "; var valueAfterRemovingSpace=removeSpacesAtTheBeginningAndTheEnd(fullName) console.log(valueAfterRemovingSpace);To run the above program, you need to use the following command −node fileName.js.Here my file name is demo208.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo208.js John Smith
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
401 Views
Let’s say the following is our array −var numbers = [10, 3, 40, 50, 20, 30, 100]We need to search two numbers from the above array elements, whose sum is 80.For this, use simple for loop with if condition.Examplefunction specificPairsOfSumOfTwoNumbers(numbers, totalValue) { var storeTwoNumbersObject = {} for(var currentNumber of numbers) { if(storeTwoNumbersObject[currentNumber]) { return { firstNumber: totalValue-currentNumber, secondNumber:currentNumber} } ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
3K+ Views
Let’s say the following is our JSON array −var details = [ { "customerDetails": [ { "customerName": "John Smith", "customerCountryName": "US" } ] }, { "customerDetails": [ { "customerName": "David Miller", "customerCountryName": "AUS" } ] }, { ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
1K+ Views
To make addEventListener() repeatedly work on button clicks, use the following code −Example Live Demo 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 ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
110 Views
Let’s say the following is our array with elements preceded by a + sign −var studentNames = [ '+John Smith', '+David Miller', '+Carol Taylor', '+John Doe', '+Adam Smith' ];To remove the + sign, the code is as follows −ExamplestudentNames = [ '+John Smith', '+David Miller', '+Carol Taylor', '+John Doe', '+Adam Smith' ]; console.log("The actual array="); console.log(studentNames); studentNames = studentNames.map(function (value) { return value.replace('+', ''); }); console.log("After removing the + symbol, The result is="); console.log(studentNames);To run the above program, you need to use the following command −node fileName.js.Here my file ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
1K+ Views
Use input type=”submit” to submit the form and another input type=”button” to clear the input on click as in the below code −Example Live Demo Document StudentName: Submit Delete function deleteSomething(secondValue) { alert("I am going to clear text box data only..."); document.getElementById("txtName").value = ""; } 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 ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
3K+ 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 Live Demo 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 ... Read More