Posts

Showing posts from August, 2016

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             alert( "No matching element found" );     }   </ script >

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 >

Reverse array in C#

To reverse an array we can use Reverse method of Array class. Overloads This method has two overloads. First overload takes an array as parameter which will be reversed. Second overload takes 3 parameters. First is array second is start index from where to start and third is length. string []arr = new string [3]; arr[0] = "One" ; arr[1] = "Two" ; arr[2] = "Three" ; Console .WriteLine( "Array before reversing" ); foreach ( string str in arr) {     Console .WriteLine(str); } Array .Reverse(arr); Console .WriteLine( "Array after reversing" ); foreach ( string str in arr) {     Console .WriteLine(str); } Output Array before reversing One Two Three Array after reversing Three Two One             

Using string array in C#

To create a stirng array we can define array of string type and length of array. string []arr = new string [5]; We can assign value to array by using index of array. Similarly we can get value by using index of array. Assigning value to array arr[0] = "One" ; arr[1] = "Two" ; arr[2] = "Three" ; arr[3] = "Four" ; arr[4] = "Five" ; Getting value from array string val = arr[2];

Get character array from string in C#

To get a  char  array from string we use ToCharArray method. Overloads This method has one overload which takes two parameters. First is StartIndex and second is Length. Return Type Return type of this method is  char  array. string str = "I like rain" ; char [] arr = str.ToCharArray(); foreach ( char c in arr) {   Console .WriteLine(c); }

Using before method in jquery

To add an html element or some text right before an html element we use   before   method of jquery. This will add the given element/text right before the element which we are targeting. < input type ="button" value =" Add Before Div " onclick =" AddBeforeDiv() " /> < div id ="myDiv" class ="myClass">     This is a div </ div > < script type ="text/javascript">     function AddBeforeDiv () {         $( "#myDiv" ).before( "<span>This is appended before div</span>" );     }; </ script >

Inserting a string within string using C#

To insert a string within a string we use Insert method . This method takes two parameters. First is start index where we want to insert. Second parameter is value which we want to insert. Return type of this method is string. Here we will insert a string at 0 index . Output is shown before and after inserting the string. string str = "like rain" ; Console .WriteLine(str); str = str.Insert(0, "I " ); Console .WriteLine(str); Output Like rain I like rain

Using after method in jquery

To add an html element or some text right after an html element we use after method of jquery. This will add the given element/text right after the element which we are targeting. < input type ="button" value =" Add After Div " onclick =" AddAfterDiv() "   /> < div id ="myDiv" class ="myClass">     This is a div </ div >   < script type ="text/javascript">     function AddAfterDiv() {         $( "#myDiv" ).after( "<This is appended after div" );     };     </ script >

Convert string to upper case and lower case

To convert a string to upper case we use ToUpper method. Return type of this method is string. To convert a string into lower case we use ToLower method. Return type of this method is string. string str = "i like rain" ; str = str.ToUpper(); Console .WriteLine(str); str = str.ToLower(); Console .WriteLine(str); Output I LIKE RAIN I like rain

Get length of a string in C#

To get length of a string we use Length property of string . This returns an integer value indicating how many characters a string contains. string str = "I like rain" ; int length = str.Length; Console .WriteLine( "Length of str is " + length); Output Length of str is 11

Using keyup in jquery

To call a method after key has been released after being pressed we can use keyup method. This method will be called after releasing the key. < input type ="text" id ="txtName" /> < script type ="text/javascript">     $( function () {         $( "#txtName" ).keyup( function () {             alert( "keyup event has fired" );         });     });     </ script >

Using change method in jquery

To call a method when value of an input element is changed we use change method. This method is only applicable to input, textarea and select elements. < input type ="text" id ="txtName" /> < script type ="text/javascript">   $( function () {         $( "#txtName" ).change( function () {             alert( "Text has changed" );         });     }); </ script >

Remove extra spaces from string in C#

Strings are commonly used in C#. Sometimes string can contain unwanted spaces in beginning or at end. To remove such spaces we use Trim method. This method removes extra spaces from start and end of string. Return type of this method is string. string Fruit = "  Apple  " ; Fruit = Fruit.Trim(); Console .WriteLine(Fruit); Output Apple

Calling blur method using jquery

To call a function when a textbox loose focus we can use blur method. This method is called as soon as focus is lost. < input type ="text" id ="txtName" /> < script type ="text/javascript">   $( function () {      $( "#txtName" ).blur( function () {         alert( "Blur method has been called" );      });  });     </ script >

To check if string ends with specific word(s)

To check if a string ends with specific word(s) we use EndsWith method. Return type of this method is Boolean. string Fruit = "Apple" ; if (Fruit.EndsWith( "e" , StringComparison .OrdinalIgnoreCase)) {     Console .WriteLine( "Fruit ends with e" ); } Output Fruit ends with e