AmitDiwan has Published 10740 Articles

Conditionally change object property with JavaScript?

AmitDiwan

AmitDiwan

Updated on 07-Sep-2020 08:00:24

1K+ Views

To conditionally change object property, use the logical AND operator ( &&).If both the operands are non-zero, then the condition becomes true in JavaScript’s logical AND operator.Examplelet marksDetails = { Mark1: 33, Mark2: 89 }, isBacklog = false; console.log("Result when backlog is set to false==="); console.log({ ...marksDetails, ...isBacklog === true ... Read More

Remove leading zeros in a JavaScript array?

AmitDiwan

AmitDiwan

Updated on 07-Sep-2020 07:58:49

421 Views

To remove leading zeroes in an array, use the concept of filter(). Following is our input wherein we will remove the leading zeroes −[10, 0, 12, 0, 0]) [0, 0, 0, 0, 0, 0, 10, 12, 0]) [12, 0, 0, 1, 0, 0])Exampleconst removeLeadingZero = input => input.filter((lastValue => value ... Read More

Change value of input on form submit in JavaScript?

AmitDiwan

AmitDiwan

Updated on 07-Sep-2020 07:57:36

6K+ Views

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 Live Demo ... Read More

What is onclick not working for two or more links for same function in JavaScript?

AmitDiwan

AmitDiwan

Updated on 07-Sep-2020 07:54:13

369 Views

If onclick not working for two or more links, then use the document.querySelectorAll() in JavaScript. Following is the code −Example Live Demo Document www.example.com www.test.com    const values = document.querySelectorAll('[class=class1]');    values.forEach(function(v) {       console.log(v.innerHTML);    }); ... Read More

Check if input is a number or letter in JavaScript?

AmitDiwan

AmitDiwan

Updated on 07-Sep-2020 07:52:13

2K+ Views

To check if the input is a number or letter, use the isNaN() function from JavaScript. It returns true if the value is NaN i.e. Not a Number. Following is the code −Example Live Demo Document Enter the value: ... Read More

Method to check if array element contains a false value in JavaScript?

AmitDiwan

AmitDiwan

Updated on 07-Sep-2020 07:47:41

227 Views

To check if array element contains a false value, you can use the Object.values() in JavaScript. Following is the code −Exampleconst details = [    {       customerDetails:       [          {             isMarried: true       ... Read More

Calling asynchronous code from index.html page in JavaScript?

AmitDiwan

AmitDiwan

Updated on 07-Sep-2020 07:37:19

296 Views

You need to use async and await with body onLoad attribute Following is the code −index.htmlExample Live Demo Document    async function callFunction(){       await testFunction();    }    function testFunction(){       console.log("Hi...");    } ... Read More

Return String by capitalizing first letter of each word and rest in lower case with JavaScript?

AmitDiwan

AmitDiwan

Updated on 07-Sep-2020 07:34:41

267 Views

For this, use toUpperCase() along with toLowerCase(). Following is the code −Examplefunction capitalEveryFirstletter(subjectTitle) {    return subjectTitle.split(' ')    .map(st => st.charAt(0).toUpperCase() + st.slice(1).toLowerCase())    .join(' ');; } var subjectTitle="iNtroduction tO JavaScript ProgRAMMINg"; var output=capitalEveryFirstletter(subjectTitle); console.log("The result="+output);To run the above program, you need to use the following command −node fileName.js.Here, ... Read More

Create global variable in jQuery outside document.ready function?

AmitDiwan

AmitDiwan

Updated on 07-Sep-2020 07:32:39

2K+ Views

To create a global variable, you need to place variable inside the tag. Following is the code −Example Live Demo Document checkGlobalVariable    var globalVariable;    $(document).ready(function() {       function initializeGlobalVariable() {          globalVariable ... Read More

How to set div position relative to another div in JavaScript?

AmitDiwan

AmitDiwan

Updated on 07-Sep-2020 07:29:16

722 Views

For this, you need to use “flex-direction” concept of CSS. Let’s say the following is our CSS style −    .demo{       display: flex;       flex-direction: column-reverse;    } Now, set the div position relative to another div −Example Live Demo Document ... Read More

Advertisements