JavaScript Archive

Introduction to JavaScript

JavaScript is a client side scripting language, it makes our HTML pages dynamic and interactive. We can write our script code directly into HTML document using <script> tag. Syntax: <script> JavaScript code here </script> Let’s take some examples: Example 1: alert(): JavaScript alert() method is used to display an popup box with a specified message

Dynamically change the form action using JavaScript

<a onclick=”$(‘form’).attr(‘action’, ‘http://bestonetechnologies.com/’); $(‘form’).submit();” title=”submit form”> on anchor tag dynamically change the form action and submit using JavaScript </a>

javascript for phone number format

Useful link for ‘javascript for phone number format’: http://www.kodyaz.com/articles/javascript-phone-format-phone-number-format.aspx

Day 14: Submit form without submit button

Sometime it’s require to submit the form on server without submit button, like on dropdown change. So this can be done with the help of JavaScript. I am taking “tbstudent” table, that we have created on “day12”. Program 41: Submit form through JavaScript <?php $con = mysql_connect(“localhost”,”root”,””); mysql_select_db(“mydb”,$con); if(isset($_REQUEST[‘drpEmp’])) { $selectedId = $_REQUEST[‘drpEmp’]; $query =

Regular Expressions in JavaScript

With the help of Regular Expressions we can make our own patterns for validation and validate form as per our requirement. This post will describe how you can make your own regular expression in JavaScript. var exp = /^ $/; Above is the syntax how to define the expression in JavaScript. Between /^ $/ we

Minimum Character Validation

In JavaScript we can check the numbers of characters entered in Text Box and validate minimum or maximum numbers of characters in textbox. example: <html> <script> function validate() { var password = document.myForm.txtPassword.value; if(password.length

Required Field Validation

One of the important use of JavaScript is Client Side Validations. We can validate the form’s data before sending to the server. example: Required Field Validation (can’t leave the field blank) <html> <script> function validate() { var name = document.myForm.txtName; if(name.value.trim()==””) { alert(“Please enter name”); name.focus(); return false; } else { return true; } }

“this” Keyword

“this” refer to the object on which a method is being invoke. We can pass “this” as an argument in function, as in given examples: example 1 <html> <head> <script type=”text/javascript”> function focusTr(objTr){ objTr.style.background =”yellow”; objTr.style.color =”red”; } function unFocusTr(objTr){ objTr.style.background =”white”; objTr.style.color =”black”; } </script> </head> <body>

Populate Div with JavaScript

We can fill div dynamically with the help of JavaScript, using innerHTML. As the example given below. <html> <head> <script type=”text/javascript”> function fillDiv(){ var sub = document.getElementById(‘msgDiv’); var txtVal = document.getElementById(‘txt’).value; sub.innerHTML = “Entered Text is “+txtVal; } </script> </head> <body> Enter Text : <input type=’text’ id=”txt”/> <input type=”button” onclick=”fillDiv()” value=”Show”/> <div id=”msgDiv”> </div> </body>

document.getElementsByName

getElementsByName() is a method in JavaScript through which we can accesses all elements with the specified name. Example : <html> <head> <script type=”text/javascript”> function getbyNameDemo(){ var sub = document.getElementsByName(‘rbtnSubjects’);