AmitDiwan has Published 10740 Articles

Check whether Enter key is pressed or not and display the result in console with JavaScript?

AmitDiwan

AmitDiwan

Updated on 03-Sep-2020 06:58:28

342 Views

For this, use onkeypress. Let’s first create input text −Now, let’s see the demoForEnterKey() function and check whether enter key is pressed or not −function demoForEnterKey(eventName) {    if (eventName.keyCode == 13) {       var t = document.getElementById("textBox");       console.log(t.value);       console.log("Enter key is ... Read More

JavaScript filter an array of strings, matching case insensitive substring?

AmitDiwan

AmitDiwan

Updated on 03-Sep-2020 06:54:01

3K+ Views

Let’s first create array of strings −let studentDetails = [    {studentName: "John Smith"},    {studentName: "john smith"},    {studentName: "Carol Taylor"} ];Now, match case insensitive substring, use the filter() and the concept of toLowerCase() in it. Following is the code −Examplelet studentDetails = [    {studentName: "John Smith"},   ... Read More

How to generate array of n equidistant points along a line segment of length x with JavaScript?

AmitDiwan

AmitDiwan

Updated on 03-Sep-2020 06:51:55

368 Views

To generate array of n equidistant points along a line segment of length x, use the below syntax −for (var anyVariableName= 0; yourVariableName< yourStartPointName; yourVariableName++) {    var v = (yourVariableName+1)/ (yourStartPointName+1);    var v2 = v*yourEndPointName;Examplefunction drawPoints(start, end) {    const arrayOfPoints = []    for (var index = ... Read More

JavaScript recursive loop to sum all integers from nested array?

AmitDiwan

AmitDiwan

Updated on 03-Sep-2020 06:49:42

439 Views

You need to call the same function again and again to sum all integers from nested array. Following is the code −Examplefunction sumOfTotalArray(numberArray){    var total= 0;    for (var index = 0; index < numberArray.length; index++) {       if (numberArray[index] instanceof Array){          total=total+sumOfTotalArray(arr[index]); ... Read More

Detect the ENTER key in a text input field with JavaScript

AmitDiwan

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) { ... Read More

Create empty array of a given size in JavaScript

AmitDiwan

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

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

AmitDiwan

AmitDiwan

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

601 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 ... Read More

Check valid date format in JavaScript?

AmitDiwan

AmitDiwan

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

702 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); ... Read More

Better ways to modify string with multiple methods using JavaScript

AmitDiwan

AmitDiwan

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

403 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 ... Read More

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

AmitDiwan

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

Advertisements