Found 9150 Articles for Object Oriented Programming

Detect the ENTER key in a text input field with JavaScript

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

6K+ Views

You can use the keyCode 13 for ENTER key. Let’s first create the input −Now, let’s use the on() with keyCode to detect the ENTER key. Following is the complete code −Example Live Demo Document    $("#txtInput").on('keyup', function (event) {       if (event.keyCode === 13) {          console.log("Enter key pressed!!!!!");       }    }); 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 ... Read More

Create empty array of a given size in JavaScript

AmitDiwan
Updated on 03-Sep-2020 06:45:54

2K+ Views

To create an empty array of given size, use the new operator −var numberArray = new Array(10);After that, let’s set some values in the array. Following is the code −Examplevar numberArray = new Array(10); console.log("The length="+numberArray.length) numberArray=[10,20,30,40,50,60]; console.log("The array value="); for(var i=0;i node demo52.js The length=10 The array value= 10 20 30 40 50 60

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

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

556 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

672 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

383 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?

Alshifa Hasnain
Updated on 14-Jan-2025 03:24:47

1K+ Views

In this article, we will learn how to iterate through all DOM elements on a webpage using JavaScript. By doing so, we can easily access and manipulate various elements of the page, whether for debugging, gathering information, or applying changes to the page's structure and content. We'll walk through a step-by-step process to loop through all elements and display the results in the console, helping you better understand how to interact with the DOM. What is the DOM? The Document Object Model (DOM) is a representation of the structure of a webpage. It treats each part of the webpage (HTML ... Read More

Transform nested array into normal array with JavaScript?

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

269 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

4K+ 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

105 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

Advertisements