Found 9150 Articles for Object Oriented Programming

How to inherit CSS properties of parent Element using JavaScript?

AmitDiwan
Updated on 01-Sep-2020 12:23:55

437 Views

You can use classList.add() along with append(). Use the document.createElement() to create a new div and the classList.add() would add the CSS class.(.class selector).Example Live Demo Document    .add-all-subject {       display: grid;       height: 100px;       width: 100px;       grid-template-columns: 2fr 2fr 2fr 2fr 2fr;       grid-template-rows: 2fr;       grid-column-gap: 5.9rem;       grid-row-gap: 2rem;       color: red;       border: 2px solid red;    } Add Subjects    const addSubjectArea = document.getElementById("add-subject"); ... Read More

How to capitalize the first letter of each word in a string using JavaScript?

AmitDiwan
Updated on 01-Sep-2020 12:18:46

3K+ Views

At first, you need to split() the string on the basis of space and extract the first character using charAt(). Use toUpperCase() for the extracted character.Examplefunction capitalizeTheFirstLetterOfEachWord(words) {    var separateWord = words.toLowerCase().split(' ');    for (var i = 0; i < separateWord.length; i++) {       separateWord[i] = separateWord[i].charAt(0).toUpperCase() +       separateWord[i].substring(1);    }    return separateWord.join(' '); } console.log(capitalizeTheFirstLetterOfEachWord("my name is john"));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo43.js.OutputThis will produce the following output with first letter capitalize −PS C:\Users\Amit\JavaScript-code> node demo43.js My Name ... Read More

How to Hide a div in JavaScript on Button Click?

AmitDiwan
Updated on 27-Jan-2025 15:00:00

6K+ Views

To hide a div in JavaScript on button click we will be discussing three different approaches with example codes. We will hide the div upon clicking the button and similarly display the hidden div upon clicking the button. In this article we are having a div element. Our task is to hide the div on clicking the button using JavaScript. Approaches to Hide div on Button Click Here is a list of approaches to hide a div in JavaScript on button click which we will be discussing in this article with stepwise explanation and complete example codes. ... Read More

Remove same values from array containing multiple values JavaScript

AmitDiwan
Updated on 01-Sep-2020 12:14:07

191 Views

Let’s say the following is our array with similar values −const listOfStudentName = ['John', 'Mike', 'John', 'Bob', 'Mike', 'Sam', 'Bob', 'John'];To remove similar values from array, use the concept of set(). Following is the code −Exampleconst listOfStudentName = ['John', 'Mike', 'John', 'Bob', 'Mike', 'Sam', 'Bob', 'John']; console.log("The value="+listOfStudentName); const doesNotContainSameElementTwice = [...new Set(listOfStudentName)]; console.log("The Array="); console.log(doesNotContainSameElementTwice)To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo42.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo42.js The value=John, Mike, John, Bob, Mike, Sam, Bob, John The Array= [ 'John', 'Mike', 'Bob', 'Sam' ]Read More

Check if value is empty in JavaScript

AmitDiwan
Updated on 01-Sep-2020 12:13:00

21K+ Views

Use the condition with “” and NULL to check if value is empty. Throw a message whenever ua ser does not fill the text box value.Example Live Demo Document USERNAME:    function checkingUserName() {       var username = document.forms["register"]["username"].value;       if (username == null || username == "") {          alert("Please enter> the username. Can’t be blank or empty !!!");          return false;       }    } To run the above program, save ... Read More

Is it possible to display substring from object entries in JavaScript?

AmitDiwan
Updated on 01-Sep-2020 12:10:22

284 Views

Yes, you can use Object.fromEntries() along with substr(). Under substr(), mention the index from where to begin the substring and the length.Exampleconst originalString = {    "John 21 2010" :1010,    "John 24 2012" :1011,    "John 22 2014" :1012,    "John 22 2016" :1013, } const result = Object.fromEntries(Object.entries(originalString). map(([k, objectValue])=> [k.substr(0, k.length-5), objectValue])); console.log(result)To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo41.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo41.js { 'John 21': 1010, 'John 24': 1011, 'John 22': 1013 }

Convert HTML table to array in JavaScript?

AmitDiwan
Updated on 01-Sep-2020 12:08:21

5K+ Views

Get data from tag using find() and store that data into array using push(). Let’s say the following is our table − Name Age John23 David26 Let’s fetch the data from and store in an array. Following is the complete code −Example Live Demo Document    .notShown {       display: none;    } Name Age John23 David26    var convertedIntoArray = [];    $("table#details tr").each(function() {       var rowDataArray = ... Read More

Count number of occurrences for each char in a string with JavaScript?

AmitDiwan
Updated on 01-Sep-2020 12:04:49

2K+ Views

Take an array to store the frequency of each character. If similar character is found, then increment by one otherwise put 1 into that array.Let’s say the following is our string −var sentence = "My name is John Smith";Following is the JavaScript code to count occurrences −Examplevar sentence = "My name is John Smith"; sentence=sentence.toLowerCase(); var noOfCountsOfEachCharacter = {}; var getCharacter, counter, actualLength, noOfCount; for (counter = 0, actualLength = sentence.length; counter < actualLength; ++counter) {    getCharacter = sentence.charAt(counter);    noOfCount = noOfCountsOfEachCharacter[getCharacter];    noOfCountsOfEachCharacter[getCharacter] = noOfCount ? noOfCount + 1: 1; } for (getCharacter in noOfCountsOfEachCharacter) {   ... Read More

Separate a string with a special character sequence into a pair of substrings in JavaScript?

AmitDiwan
Updated on 01-Sep-2020 12:03:44

693 Views

Let’s say we have the following string with special character sequence −var fullName=" John Smith ";To separate the above string into substring, use regex and then split(). The syntax is as follows −var anyVariableName=(/\s*\s*/g); var anyVariableName=yourVariableName.trim().split(yourVariableName);Following is the complete JavaScript code −Examplevar fullName=" John Smith "; console.log("The Value="+fullName); var regularExpression=(/\s*\s*/g); var seprateName=fullName.trim().split(regularExpression); console.log(seprateName);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo39.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo39.js The Value= John Smith [ 'John', 'Smith' ]Read More

How to round the decimal number to the nearest tenth in JavaScript?

AmitDiwan
Updated on 01-Sep-2020 11:56:30

598 Views

To round the decimal number to the nearest tenth, use toFixed(1) in JavaScript. The syntax is as follows −var anyVaribleName=yourVariableName.toFixed(1)Let’s say the following is our decimal number −var decimalValue =200.432144444555; console.log("Actual value="+decimalValue)Let’s now round the decimal number. Following is the code −Examplevar decimalValue =200.432144444555; console.log("Actual value="+decimalValue) var modifiedValue=decimalValue.toFixed(1) console.log("Modified value="+ modifiedValue);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo38.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo38.js Actual value=200.432144444555 Modified value=200.4Read More

Advertisements