Found 9313 Articles for Object Oriented Programming

How do I run two or more functions when using 'onclick' JavaScript?

AmitDiwan
Updated on 03-Sep-2020 06:44:39

441 Views

Let’s first set a button − Call Above, we have set a function under “onclick” to call two other functions −function callTwoOtherFunctions(){    fun1();    fun2(); }In this way, work around the fun1() and fun2() as in the complete code below −Example Live Demo Document Call    function callTwoOtherFunctions(){       fun1();       fun2();    }    function fun1(){       console.log("Function1()")    }    function fun2(){       console.log("Function2()")    } To run the above program, save the file name “anyName.html(index.html)” ... Read More

Check valid date format in JavaScript?

AmitDiwan
Updated on 03-Sep-2020 06:41:38

582 Views

To check for valid date format, match the date with −const dateFormat = /^\d{4}\-\d{2}\-\d{2}$/;Example Live Demo Document    .check-valid-date {       border: 1px solid red;    }    const dateFormat = /^\d{4}\-\d{2}\-\d{2}$/;    document.getElementById("check-valid-date").addEventListener("change",    checkingForValidDate);    function checkingForValidDate() {       console.log(this.value, dateFormat.test(this.value));       this.classList.toggle('check-valid-date',       dateFormat.test(this.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 ... Read More

Better ways to modify string with multiple methods using JavaScript

AmitDiwan
Updated on 03-Sep-2020 06:37:11

339 Views

To modify string, you can use toLowerCase() as well as toUpperCase(). Let’s say the following is our string −var sentence = "tHIS iS tHE JavaScript pROGRAM";To modify and display in proper case, the code is as follows −Examplevar sentence = "tHIS iS tHE JavaScript pROGRAM"; function modifyStringWithMultipleMethods(sentence) {    return sentence.charAt(0).toUpperCase() +    sentence.slice(1).toLowerCase(); } console.log(modifyStringWithMultipleMethods(sentence));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo51.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo51.js This is the JavaScript programRead More

Remove any text not inside element tag on a web page with JavaScript?

AmitDiwan
Updated on 03-Sep-2020 06:32:47

2K+ Views

To remove text, use the concept of remove(). Use filter to get the content not inside element tag.Let’s say the following is our HTML −Demo Program This is also Demo ProgramAnd we have to remove “This is also Demo Program” since it is not under element tag. For that, the compete code is as follows using filter() and remove() −Example Live Demo Document Demo Program This is also Demo Program    $('body').contents().filter(function(){       return this.nodeType != 1;    }).remove(); To run the above program, save the file ... Read More

JavaScript: How to loop through all DOM elements on a page and display result on console?

AmitDiwan
Updated on 03-Sep-2020 06:30:35

824 Views

To loop through all DOM elements on a page, use document.getElementsByTagName(‘*’). Loop through its length and display the result in console as in the below code −var tags = document.getElementsByTagName("*"); for (var i=0, max=tags.length; i < max; i++) {    console.log(tags[i]); }Example Live Demo Document Demo Enter the name: Save    var tags = document.getElementsByTagName("*");    for (var i=0, max=tags.length; i < max; i++) {       console.log(tags[i]);    } To run the above program, save the file name “anyName.html(index.html)” and right click on the ... Read More

Transform nested array into normal array with JavaScript?

AmitDiwan
Updated on 03-Sep-2020 06:27:27

193 Views

Let’s say the following is our nested array −const arrayObject = [    [       {          Name: "John"       },       {          countryName: "US"       }    ],    [       {          subjectName: "JavaScript"       },       {          teacherName: "Mike"       }    ] ];To transform nested array into normal array, use the concept of flat() as in the below code −Exampleconst arrayObject = [    [       {          Name: "John"       },       {          countryName: "US"       }    ],    [       {          subjectName: "JavaScript"       },       {          teacherName: "Mike"       }    ] ]; const output = arrayObject.flat(); console.log(output);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo50.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo50.js [    { Name: 'John' },    { countryName: 'US' },    { subjectName: 'JavaScript' },    { teacherName: 'Mike' } ]

Getting an HTML H1 value to JavaScript variable?

AmitDiwan
Updated on 03-Sep-2020 06:25:33

3K+ Views

To get the value of H1 to JavaScript variable, you can use −document.getElementById().innerHTML.Let’s say the following is our H1 heading − This is the demo program of JavaScript ........Now, let’s get the H1 value using the below code −Example Live Demo Document This is the demo program of JavaScript ........    var data=document.getElementById('demo').innerHTML;    console.log("The data is="+data); 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 ... Read More

How to get only the first BOT ID from thes JavaScript array?

AmitDiwan
Updated on 03-Sep-2020 06:21:00

68 Views

Let’s say we have records with BOTID and Name of assigned users −let objectArray = [    { BOTID: "56", Name: "John" },    { BOTID: "57", Name: "David" },    { BOTID: "58", Name: "Sam"},    { BOTID: "59", Name: "Mike" },    { BOTID: "60", Name: "Bob" } ];We know the array starts from index 0. If you want to access the first element from the above array, use the below syntax −var anyVariableName=yourArrayObjectName[index].yourFieldName;Examplelet objectArray = [    { BOTID: "56", Name: "John" },    { BOTID: "57", Name: "David" },    { BOTID: "58", Name: "Sam"},   ... Read More

How would I add a newline in an Unordered List (UL) from JavaScript?

AmitDiwan
Updated on 03-Sep-2020 06:19:46

273 Views

To add a newline in Unordered List, use the document.querySelector().append(). Following is the code −Example Live Demo Document    h1{       font-size: 2.50rem;    }    h2, label{       font-size: 1.50rem;    } Adding Name Demo Enter The Name: Save List Of Name    const buttonName = document.querySelector('.btnName')    const addName = e => {       let nameTxt = document.querySelector('.txtName'),       name = nameTxt.value.trim()       if (name) {          let tagLi = ... Read More

How can I instantiate a dictionary in JavaScript where all keys map to the same value?

AmitDiwan
Updated on 03-Sep-2020 06:14:35

233 Views

At first, set the keys −const name = ['Name1', 'Name2'];Now, map keys to the same value using for loop as in the below code −Exampleconst name = ['Name1', 'Name2']; const keyValueObject = {}; for (const k of name){    keyValueObject[k] = 'John'; } console.log(keyValueObject);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo48.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo48.js { Name1: 'John', Name2: 'John' }

Advertisements