AmitDiwan has Published 10744 Articles

Display a message on console while focusing on input type in JavaScript?

AmitDiwan

AmitDiwan

Updated on 11-Sep-2020 08:27:14

253 Views

For this, you can use the concept of focus(). Following is the code −Example Live Demo Document Submit    const submitButton = document.getElementById('submitBtn');    submitButton.addEventListener('click', () => {       console.log('Submitting information to the server.....');    })   ... Read More

Filter null from an array in JavaScript?

AmitDiwan

AmitDiwan

Updated on 11-Sep-2020 08:23:15

1K+ Views

To filter null from an array and display only non-null values instead, use filter(). Following is the code −Examplevar names=[null, "John", null, "David", "", "Mike", null, undefined, "Bob", "Adam", null, null]; console.log("Before filter null="); console.log(names); var filterNull=[]; filterNullValues=names.filter(obj=>obj); console.log("After filtering the null values="); console.log(filterNullValues);To run the above program, you need ... Read More

Retrieving object's entries in order with JavaScript?

AmitDiwan

AmitDiwan

Updated on 11-Sep-2020 08:21:14

254 Views

Let’s say the following is our object −const subjectDetails ={    102:"Java",    105:"JavaScript",    104:"MongoDB",    101:"MySQL" };Use sort() method to retrieve object’s entry in order −const orderSubjects = Object.fromEntries(Object.keys(subjectDetails).sort().map((k) => {    return [k, subjectDetails[k]]; }));Exampleconst subjectDetails ={    102:"Java",    105:"JavaScript",    104:"MongoDB",    101:"MySQL" }; const ... Read More

Print JSON nested object in JavaScript?

AmitDiwan

AmitDiwan

Updated on 11-Sep-2020 08:20:11

1K+ Views

To print JSON nested object in JavaScript, use for loop along with JSON.parse(). Following is the code −Examplevar details = [    {       "studentId": 101,       "studentName": "John",       "countryName": "US",       "subjectDetails": "{\"0\":\"JavaScript\", \"1\":\"David\"}"    },    {     ... Read More

Getting HTML form values and display on console in JavaScript?

AmitDiwan

AmitDiwan

Updated on 11-Sep-2020 08:18:51

6K+ Views

To get HTML form values, use the value property. Let’s say the following is our input type −We need to display the above value “My name is John Smith” on console.Example Live Demo Document    var originalvalue = document.getElementById("getValues").value; ... Read More

Find the number of times a value of an object property occurs in an array with JavaScript?

AmitDiwan

AmitDiwan

Updated on 11-Sep-2020 08:16:49

121 Views

For this, use the concept of reduce(). Following is the code −Exampleconst subjectDetails = [    {       subjectId: '101',       subjectName: 'JavaScript'    },    {       subjectId: '102',       subjectName: 'Java'    },    {       subjectId: '103', ... Read More

How to add and remove names on button click with JavaScript?

AmitDiwan

AmitDiwan

Updated on 11-Sep-2020 08:14:55

3K+ Views

To create, use add() method, whereas to delete created and appended element, you can use remove(). Following is the code −Example Live Demo Document Add A New Name AddName ... Read More

Remove Seconds/ Milliseconds from Date and convert to ISO String?

AmitDiwan

AmitDiwan

Updated on 11-Sep-2020 08:10:43

3K+ Views

Let’s get the current date first −var currentDate = new Date(); console.log("The current date is as follows="+currentDate);Now, let us remove seconds/ milliseconds by setting them to 0 using setSeconds() −currentDate.setSeconds(0, 0);Convert to ISO String using toISOString() −currentDate.toISOString()Let us now see the complete code with output −Examplevar currentDate = new Date(); ... Read More

Toggle hide class only on selected div with JavaScript?

AmitDiwan

AmitDiwan

Updated on 11-Sep-2020 08:03:14

519 Views

To toggle hide class only on selected div, you need to set event on click button. Let’s say you need to hide a specific div on the click of + sign.To get font + or - icon, you need to link font-awesome −Following is the code to toggle hide class ... Read More

How to parse float with two decimal places in JavaScript?

AmitDiwan

AmitDiwan

Updated on 11-Sep-2020 07:07:48

592 Views

To parse float with two decimal places, use the concept of toFixed(2). Following is the code −Examplevar price = 178932.89; var tax = 7.9; var totalPrice = price*tax; console.log("The actual value="+totalPrice); var gettingTwoPlacedValues = parseFloat(totalPrice).toFixed(2); console.log("After getting two placed values="+gettingTwoPlacedValues);To run the above program, you need to use the following ... Read More

Advertisements