Yes, select text box with JavaScript using the select() method. At first, let us create an input text −Enter your Name: Select Text BoxNow, let us select the text box on button click −Example Live Demo Document Enter your Name: Select Text Box function check(){ document.getElementById("txtName").select(); } 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 output −When you click ... Read More
To replace a value if null or undefined, you can use below syntax −var anyVariableName= null; var anyVariableName= yourVariableName|| anyValue;Examplevar value = null; console.log("The value ="+value) var actualValue = value || "This is the Correct Value"; console.log("The value="+actualValue);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo56.jsOutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo56.js The value =null The value=This is the Correct Value
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 pressed.....") return true; } else { console.log("Enter key is not pressed.....") return false; } }Example Live Demo Document function demoForEnterKey(eventName) { if (eventName.keyCode == 13) ... Read More
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"}, {studentName: "john smith"}, {studentName: "Carol Taylor"} ]; var searchName="John Smith" console.log(studentDetails.filter(obj => obj.studentName.toLowerCase().indexOf(searchName.toLowerCase()) >= 0));To run the above program, you need to use the following command;node fileName.js.Here, my file name is demo55.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo55.js [ { studentName: 'John Smith' }, { studentName: ... Read More
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 = 0; index < start; index++) { var v = (index+1)/ (start+1); var v2 = v*end; arrayOfPoints.push(Math.round(v2)); } return arrayOfPoints; } const arrayOfPoints = drawPoints(5, 50); console.log("The Points="); console.log(arrayOfPoints);To run the above program, you need to use the following command ... Read More
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]); } if (numberArray[index] === Math.round(numberArray[index])){ total=total+numberArray[index]; } } return total; } var number = new Array(6); number=[10, 20, 30, 40, 50, 60]; console.log("The sum is="+sumOfTotalArray(number));To run the above program, you need to use the following ... Read More
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
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
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
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