Web Development Articles - Page 503 of 1049

How to make Format ABC-1234 in JavaScript regular Expressions?

AmitDiwan
Updated on 16-Jul-2020 12:39:58

290 Views

Following is the JavaScript code to make a format like ABC-1234 −Example Live Demo Document ABC1234    function joinTheDashSymbol(m, r1,r2,r3,r4,r5) {       if (r1)          return [r1,r2,r3].join('-');       else          return [r4,r5].join('-');    }    $(".formatting").text(function(i, words) {       words = words.replace(/^(\d{3})(\d{3})(\d{4})|([A-Z]{3})(\d{4})$/,       joinTheDashSymbol);       return words;    }); To run the above program, save the file name anyName.html(index.html) and right click on the file and select the option open with live server in VS code editor.Output

Adding default search text to search box in HTML with JavaScript?

Vivek Verma
Updated on 28-Aug-2025 16:10:11

521 Views

A default search text is a text that provides suggestions or a hint of what to search for in the search (input) box. Following is an illustration of a search box, where you can observe a default search text "Search your favorite tutorials…" : The above default search text will suggest to visitors that they can search for their favorite tutorial, which they want to read or open on the website. Adding Default Search Text to Search Box Using JavaScript In HTML, the text (i.e., search text) that appears inside a search box or input field before the user ... Read More

Get global variable dynamically by name string in JavaScript?

AmitDiwan
Updated on 16-Jul-2020 12:35:49

485 Views

Display in alert by using alert(window()). Following is the syntax −alert(window['yourVariableName' + 'yourVariableName' + otherVariableName]);Example Live Demo Document    var globalVariablelocal_print100=100;    print100=1;    alert(window['globalVariable' + 'local_' + print100]); To run the above program, save the file name anyName.html(index.html) and right click on the file and select the option open with live server in VS code editor.Output

How to output JavaScript into a Textbox?

AmitDiwan
Updated on 16-Jul-2020 12:49:59

4K+ Views

You can use the concept of value(). Following is the JavaScript code −Example Live Demo Document FirstNumber SecondNumber: Add Two Nnumbers The addition is:    function add() {       var f = parseInt(document.getElementsByName('input1')[0].value);`       var s =parseInt(document.getElementsByName('input2')[0].value);       var result = f+s;       document.getElementsByName('display')[0].value= result;    } To run the above program, save the file name anyName.html(index.html) and right click on the file and select the option open with live server in VS code editor.OutputOn clicking the button, the value will be displayed in the textbox. The snapshot is as follows −

Object de-structuring in JavaScript.

AmitDiwan
Updated on 16-Jul-2020 08:57:01

124 Views

Following is the code for object de-structuring in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .sample, .result {       font-size: 18px;       font-weight: 500;       color: red;    }    .result {       color: rebeccapurple;    } Object de-structuring in JavaScript. CLICK HERE Click on the above button to destructure the object above    let sampleEle = document.querySelector(".sample");    let resultEle = document.querySelector(".result");    let obj = { ... Read More

Array.prototype.sort() in JavaScript.

AmitDiwan
Updated on 16-Jul-2020 08:52:17

170 Views

The JavaScript Array.prototype.sort() method is used for sorting an array. The order of sorting can be alphabetic, numeric, ascending or descending.Following is the code for the Array.prototype.sort() method −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .sample, .result {       font-size: 18px;       font-weight: 500;       color: red;    }    .result {       color: rebeccapurple;    } JavaScript Array.prototype.sort() method CLICK HERE Click on the above button to sort the ... Read More

Array.prototype.flat() in JavaScript.

AmitDiwan
Updated on 16-Jul-2020 08:47:46

131 Views

The JavaScript Array.prototype.flat() method is used to flatten an array recursively up to a specified depth. It doesn’t manipulate the original array but creates a new flattened array.Following is the code for the Array.prototype.flat() method −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result,.sample {       font-size: 20px;       font-weight: 500;    } Array.prototype.flat() method {       let store = arr.flat(1);       store.forEach((item,index) => {          resEle.innerHTML += index + ' : ' + item + '';       });    }); OutputThe above code will produce the following output −On clicking the ‘CLICK HERE’ button −

Array.prototype.flatMap() in JavaScript.

AmitDiwan
Updated on 16-Jul-2020 08:45:19

143 Views

The JavaScript Array.prototype.flatMap() function flattens the given nested array into a new flat array.Following is the code for the Array.prototype, flatMap() method −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .sample {       font-size: 20px;       font-weight: 500;    } JavaScript Array flatMap() CLICK HERE Click on the above button to convert the nested array to flat array    let fillEle = document.querySelector(".sample");    let arr = ["cow", ["bull", "lion", "tiger"], "sheep"];    for (x ... Read More

Explain Optional Catch Binding in JavaScript.

AmitDiwan
Updated on 16-Jul-2020 08:41:45

441 Views

The optional catch binding introduced in ES2019 allows us to remove the surrounding parentheses of a catch binding i.e. we don’t need to use a variable to store the error object. It is useful especially if we know about the error in advance or even if want to react to an error without knowing about it.Following is the code for the optional catch binding in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 20px;       ... Read More

Explain String.trimStart() & String.trimEnd() methods in JavaScript

AmitDiwan
Updated on 16-Jul-2020 08:38:55

495 Views

The String.trimStart() method is used to remove whitespace from the string beginning while the String.trimEnd() method removes whitespace from the end of the string.Following is the code for the String.trimStart() and String.trimEnd() methods −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result,.sample {       font-size: 20px;       font-weight: 500;    } String.trimStart() and String.trimEnd() methods {       resEle.innerHTML = "trimStart() :" + str.trimStart() + "|";       resEle.innerHTML += "trimEnd() :" + str.trimEnd() + "|";    }); OutputThe above code will produce the following output −On clicking the ‘CLICK HERE’ button −

Advertisements