AmitDiwan has Published 10740 Articles

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

714 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

646 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

876 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

323 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

703 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

550 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

603 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

How to find all subsets of a set in JavaScript?

AmitDiwan

AmitDiwan

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

706 Views

To find all subsets of a set, use reduce() along with map() in JavaScript. Let’s say, we are passing the set [8, 9] and finding the subsets.Exampleconst findAllSubsetsoOfGivenSet = originalArrayValue => originalArrayValue.reduce(    (givenSet, setValue) => givenSet.concat(       givenSet.map(givenSet => [setValue, ...givenSet])    ), [[]] ); console.log(findAllSubsetsoOfGivenSet([8, 9]));To ... Read More

Advertisements