AmitDiwan has Published 10744 Articles

Count number of occurrences for each char in a string with JavaScript?

AmitDiwan

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

Separate a string with a special character sequence into a pair of substrings in JavaScript?

AmitDiwan

AmitDiwan

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

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

How to round the decimal number to the nearest tenth in JavaScript?

AmitDiwan

AmitDiwan

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

598 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="+ ... Read More

How to get value from the first checkbox which is not hidden in jQuery?

AmitDiwan

AmitDiwan

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

850 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; } ... Read More

Store and retrieve arrays into and from HTML5 data attributes with jQuery?

AmitDiwan

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

Simplest way to detect keypresses in JavaScript?

AmitDiwan

AmitDiwan

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

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

How to find out what character key is pressed in JavaScript?

AmitDiwan

AmitDiwan

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

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

Avoid Unexpected string concatenation in JavaScript?

AmitDiwan

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

How do I display only the visible text with jQuery?

AmitDiwan

AmitDiwan

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

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

Parse JSON in JavaScript to display a specific name/value pair?

AmitDiwan

AmitDiwan

Updated on 01-Sep-2020 11:32:25

575 Views

To parse JSON, use parseJSON() and for displaying a specific pair, use the $.each() function.Let’s say the following is our JSON, which is parsed −const APIData = '[{"Name":"John", "Age":21}, {"Name":"David", "Age":24}, {"Name":"Bob", "Age" :20}]'; const getObject = jQuery.parseJSON(APIData);Now, let’s see the complete code and fetch the “Name” pairs −Example Live Demo ... Read More

Advertisements