Convert HTML Table to Array in JavaScript

AmitDiwan
Updated on 01-Sep-2020 12:08:21

5K+ Views

Get data from tag using find() and store that data into array using push(). Let’s say the following is our table − Name Age John23 David26 Let’s fetch the data from and store in an array. Following is the complete code −Example Live Demo Document    .notShown {       display: none;    } Name Age John23 David26    var convertedIntoArray = [];    $("table#details tr").each(function() {       var rowDataArray = ... Read More

Count Number of Occurrences for Each Character in a String with JavaScript

AmitDiwan
Updated on 01-Sep-2020 12:04:49

2K+ Views

Take an array to store the frequency of each character. If similar character is found, then increment by one otherwise put 1 into that array.Let’s say the following is our string −var sentence = "My name is John Smith";Following is the JavaScript code to count occurrences −Examplevar sentence = "My name is John Smith"; sentence=sentence.toLowerCase(); var noOfCountsOfEachCharacter = {}; var getCharacter, counter, actualLength, noOfCount; for (counter = 0, actualLength = sentence.length; counter < actualLength; ++counter) {    getCharacter = sentence.charAt(counter);    noOfCount = noOfCountsOfEachCharacter[getCharacter];    noOfCountsOfEachCharacter[getCharacter] = noOfCount ? noOfCount + 1: 1; } for (getCharacter in noOfCountsOfEachCharacter) {   ... Read More

Separate String into Substrings in JavaScript

AmitDiwan
Updated on 01-Sep-2020 12:03:44

703 Views

Let’s say we have the following string with special character sequence −var fullName=" John Smith ";To separate the above string into substring, use regex and then split(). The syntax is as follows −var anyVariableName=(/\s*\s*/g); var anyVariableName=yourVariableName.trim().split(yourVariableName);Following is the complete JavaScript code −Examplevar fullName=" John Smith "; console.log("The Value="+fullName); var regularExpression=(/\s*\s*/g); var seprateName=fullName.trim().split(regularExpression); console.log(seprateName);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo39.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo39.js The Value= John Smith [ 'John', 'Smith' ]Read More

Round Decimal Number to Nearest Tenth in JavaScript

AmitDiwan
Updated on 01-Sep-2020 11:56:30

611 Views

To round the decimal number to the nearest tenth, use toFixed(1) in JavaScript. The syntax is as follows −var anyVaribleName=yourVariableName.toFixed(1)Let’s say the following is our decimal number −var decimalValue =200.432144444555; console.log("Actual value="+decimalValue)Let’s now round the decimal number. Following is the code −Examplevar decimalValue =200.432144444555; console.log("Actual value="+decimalValue) var modifiedValue=decimalValue.toFixed(1) console.log("Modified value="+ modifiedValue);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo38.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo38.js Actual value=200.432144444555 Modified value=200.4Read More

Get Value from the First Visible Checkbox in jQuery

AmitDiwan
Updated on 01-Sep-2020 11:55:11

857 Views

To get value from the first checkbox, which is not hidden, use the :visible selector. Following is the code −Example Live Demo >Document    .notShown {       display: none; }    var v=$('input:checkbox:checked:visible:first').val();    console.log(v); 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.This will produce the following output displaying the visible value “second” in console −

Store and Retrieve Arrays in HTML5 Data Attributes with jQuery

AmitDiwan
Updated on 01-Sep-2020 11:51:35

2K+ Views

To store and retrieve arrays into and from data attributes, use the data() method in jQuery. Following is the syntax −var anyVariableName= $('#yourIdName).data('yourJavscriptArrayName');Following is the jQuery code −Example Live Demo Document    var value = $('#test').data('details');    alert(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 the following output −

Detect Keypresses in JavaScript

AmitDiwan
Updated on 01-Sep-2020 11:41:43

313 Views

The easiest way to detect keypresses in JavaScript, use the onKeyPress event handler −document.onkeypressThe key press is matched with the keyCode property, which returns the Unicode character code of the key that triggered the onkeypress event.Example Live Demo Document    document.onkeypress = function (eventKeyName) {       eventKeyName = eventKeyName || window.event;       if(eventKeyName.keyCode==13){          console.log('You have pressed enter key');       } else {          alert(String.fromCharCode(eventKeyName.keyCode))    } }; To run the above program, save the file name ... Read More

Find Out What Character Key is Pressed in JavaScript

AmitDiwan
Updated on 01-Sep-2020 11:38:44

690 Views

To find out which characters key is pressed, use the window.event along with keyCode. Following is the code −Example Live Demo Document    function keyPressName(myEventKeyName){       var pressedKey;       if(window.event){          pressedKey = myEventKeyName.keyCode;       } else if(myEventKeyName.which)       {          pressedKey = myEventKeyName.which;    }    alert(String.fromCharCode(pressedKey)); } 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” ... Read More

Avoid Unexpected String Concatenation in JavaScript

AmitDiwan
Updated on 01-Sep-2020 11:35:45

2K+ Views

To avoid unexpected string concatenation while concatenating strings, multiple strings, and numbers, use backticks.We have the following −const concatValue = 'John, David, Mike'; var friendNames= `${concatValue}`;The above value is concatenated with a string and number −var studentNameWithFriends=` ${concatValue}| 'Carol' | 24 ` ;Following is the complete JavaScript code for concatenation −Exampleconst concatValue = 'John, David, Mike'; var friendNames= `${concatValue}`; var studentNameWithFriends=` ${concatValue}| 'Carol' | 24 ` ; console.log(friendNames); console.log(studentNameWithFriends);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo37.jsOutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo37.js John, David, Mike John, ... Read More

Display Only Visible Text with jQuery

AmitDiwan
Updated on 01-Sep-2020 11:34:16

533 Views

To display only the visible text, use the concept of − visible selector in jQuery. It selects the element currently visible. Following is the code −Example Live Demo Document Test Class Demo class    $('#myDiv').children(":visible").text() 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.This will produce the following output displaying the visible text −

Advertisements