Posts

Showing posts with the label Javascript

Get elements by name in javascript

Getting elements by name attribute can done using getElementsByName method of document. This method takes value of name attribute as argument.  It returns array of elements because more than one element(s) can have same name. < input type ="button" value ="Get By Name" onclick =" GetByName() " /> < input type ="text" name ="txtNumber" /> < input type ="text" name ="txtNumber" /> < input type ="text" name ="txtNumber" /> < script type ="text/javascript">    function GetByName() {         var elements = document.getElementsByName( "txtNumber" );         if (elements.length > 0) {             alert(elements[0].value);         }         else           ...

Get elements by class name in javascript

To get elements using class name we use getElementsByClassName method available with document object. This method takes name of class an argument.  It returns array of elements because more than one element(s) can have same class. < input type ="button" value ="Get By Class" onclick =" GetByClassName() " /> < div id ="myDiv" class ="myClass">     This is a div </ div > < script type ="text/javascript">   function GetByClassName() {         var elements = document.getElementsByClassName( "myClass" );         if (elements.length > 0) {             alert(elements[0].innerHTML);         }     else       alert( "No matching element found" );    }   </ script >

Getting element by id in javascript

To get an element by its id we use getElementById method available with document object. It takes Id of element which we want to target. If element is not found it returns null. < input type ="button" value ="Get By Id" onclick =" GetByID() " /> < div id ="myDiv">     This is a div </ div > < script type ="text/javascript"> function GetByID() {   var element = document.getElementById( "myDiv" );    if (element != null ) {    alert(element.innerHTML);   } } </ script >

Getting value of textbox using javascript/jquery

Javascript and jquery are commonly used for client side scripting. Here we will see how we can get value of textbox using javascript and jquery. Html < input type ="number" id ="txtAge" /> Using Javascript < script type ="text/javascript">       var age = document.getElementById( "txtAge" ).value; </ script > Jquery < script type ="text/javascript"> var age = $( "#txtAge" ).val(); </ script >