Category: javascript

arrays in javascript

In this example, we learn how to create an array in javascript and how to traverse array. How to perform various built-in operations on array elements, such as sorting, etc. <html> <head> <title></title> <script language="javascript" type="text/javascript"> var instruments = new Array("piano", "drum", "flute"); //document.write(instruments.length); //document.write(instruments.toString()); var myarray = new Array(); myarray[0] = "pen"; myarray[1] =

Date operations using javascript

In this code, we use javascript to perform various operations on date <html> <head> <title></title> <script language="javascript" type="text/javascript"> var today = new Date(); //document.write(today); var newYear = new Date(2014,4,1); document.write(newYear.getDate()); var daysRemaining; daysRemaining = today - newYear ; //document.write(daysRemaining/86400000); </script> </head> <body> </body> </html>  

disable form posting using javascript

Sometimes we do not want user to submit form unless some conditions are met. In the following example, the user will be able submit form only if (s)he clicks on “I agree” checkbox. <!DOCTYPE html> <html> <head> <title>Untitled document</title> <meta charset="utf-8" /> <script language="javascript" type="text/javascript"> function enableButton() { var chk = document.getElementById("chkagree"); var btn =

validating html object array using javascript

In this example, we validate an array of checkbox, text, and radio fields. We also validate a html drop down “select” <!DOCTYPE html> <html> <head> <title>Untitled document</title> <meta charset="utf-8" /> <script language="javascript" type="text/javascript"> function validate() { var currency = document.getElementById("txtcurrency").value; var country = document.getElementById("ddlcountry"); var radsuit = document.getElementsByName("radSuit"); var chkSuit = document.getElementsByName("chkSuit"); var status=false; var

replacing words in paragraph using javascript

Sometimes it is important to replace some words from a paragraph. This can be done at client end using javascript. In the following example, we want to replace two words “class” and “javascript” from the text written in textarea. The words are replaced with “***” strings. <!DOCTYPE html> <html> <head> <title>Untitled document</title> <meta charset="utf-8" />

javascript validating required fields

One way to perform check on all the required input fields is by assigning a class name in the “class” property, and check all those fields whose “class” property is set with value “required”. The following example demonstrates that. <!DOCTYPE html> <html> <head> <title>Untitled document</title> <meta charset="utf-8" /> <style> .required { } </style> <script language="javascript"

javascript based entry checks with messages

In the following example, we perform data entry checks and highlight the input fields whose values are not provided or not provided in proper format <!DOCTYPE html> <html> <head> <title>Untitled document</title> <meta charset="utf-8" /> <style> .normal { background-color:white; } .error { background-color:orangered; } </style> <script language="javascript" type="text/javascript"> function validate() { var name = document.getElementById("txtname"); var

javascript based form validation

In this example, we write javascript code to validate the input entries of a form. <!DOCTYPE html> <html> <head> <title>Untitled document</title> <meta charset="utf-8" /> <script language="javascript" type="text/javascript"> function validate() { var name = document.getElementById("txtname"); var passwd = document.getElementById("txtpasswd"); var cpasswd = document.getElementById("txtcpasswd"); if (name.value.length == 0) { alert("Name cannot be blank"); name.focus(); return false; }

highlighting text using javascript

In the following example, we call a javascript function highlight the text in a textbox. The code also changes the color of document on page load using a custom javascript function changeColor() <!DOCTYPE html> <html> <head> <title>Untitled document</title> <meta charset="utf-8" /> <script language="javascript" type="text/javascript"> function changeColor() { document.bgColor = 'green'; } function myfunction1() { document.getElementById("txt").select();