In JavaScript, we can use document.querySelector() to check if an element exists. The document.querySelector() searches for and returns the first element matching a selector or set of selectors. If it finds no matching elements, it returns null. For example, to check if an element with the id elementId exists: if (document.querySelector("#elementId")) { // The element […]

Read more Check if element exists in JavaScript

In JavaScript, we can use the built-in filter() function to remove a specified item from an array. Table of contents: 1. Remove specified item from the array with filter() 2. Remove two specified items from the array with filters() and includes() 3. JavaScript examples 4. References 1. Remove specified item from the array with filter() […]

Read more JavaScript – Remove a specified item from array

A JavaScript example to show you how to get the selected value or text from a dropdown list. A drop box list. <select id="country"> <option value="None">– Select –</option> <option value="ID001">China</option> <option value="ID002" selected>United State</option> <option value="ID003">Malaysia</option> </select> Get option value : var e = document.getElementById("country"); var result = e.options[e.selectedIndex].value; alert(result); //ID002 Get option text : […]

Read more JavaScript – Get selected value from dropdown list

In JavaScript, we can use either location.replace() or location.href to redirect a page. Normally, we use location.replace() to simulate an HTTP redirect. <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> </head> <body> <h1>JavaScript – How to redirect a page</h1> <h3>1. location.replace</h3> <button onclick="httpRedirect()">location.replace</button> <h3>2. location.href</h3> <button onclick="mouseClick()">location.href</button> <script> // Simulate an HTTP redirect function httpRedirect() { location.replace("https://www.mkyong.com") […]

Read more JavaScript – How to redirect a page

For security concern, many users like to disable JavaScript in their web browser, and it raise a big problem in many website which depends on JavaScript to function well. The easy and simple solution is using the HTML “noscript” tag to display different content if JavaScript is disabled. See following full “noscript” example : If […]

Read more Display different content if JavaScript is disabled

In JavaScript, you can use following two ways to get hidden field value in a form : document.getElementById(‘hidden field id’).value document.formName.elements[‘hidden field name’].value See an example here… <html> <body> <script type="text/javascript"> function printIt(){ alert(document.getElementById(‘abcId’).value); alert(document.formName.elements[‘abcName’].value); } </script> <h1>Access Hidden value in JavaScript</h1> <form name="formName"> <input type=hidden id="abcId" name="abcName" value="Wall Street! Money Never Sleep"/> <input type=button […]

Read more How to get hidden field value in JavaScript

Often times, we need to “preload” an image into browser’s cache for performance concern. For instance, we have a mouseover event and the image needs to be update without any delay. This “Preloading” stuff can be achieved in two ways , either JavaScript or HTML code. Javascript We can “preload” an image with the following […]

Read more How to preload an image in JavaScript or HTML code?

The getElementsByName() method is use to get the element by name. However be aware of the getElementsByName() method always return an array as output. Caution The method always return an array, and we have to use [] to access the value. For example alert(document.getElementsByName(“myInput”).value); It will prompt out an undefined value alert(document.getElementsByName(“myInput”)[0].value); It will prompt […]

Read more How to get element by name in HTML – getElementsByName

In JavaScript, you can use getElementById() fucntion to get any prefer HTML element by providing their tag id. Here is a HTML example to show the use of getElementById() function to get the DIV tag id, and use InnerHTML() function to change the text dynamically. HTML… <html> <head> <title>getElementById example</title> <script type="text/javascript"> function changeText(text) { […]

Read more How to get div id element in Javascript

Many webmasters like to disable the right click button to protect their own content. Here is the JavaScript to disable right click button on a website : <script> <!– //edit this message to say what you want var message = "Function Disabled"; function clickIE() { if (document.all) { alert(message); return false; } } function clickNS(e) […]

Read more JavaScript – How to disable right click button on a website