Posts

Add click event using jquery

To add click event using jquery we use . click method of jquery. We can add click event on page load but we can also add event whenever needed. < input type ="button" value ="Click me" id ="btnBindClick" /> < script type ="text/javascript">     $( function () {         $( "#btnBindClick" ).click( function () {             alert( "Click is binded using jquery" );         });     });     </ script >

To check if string starts with specific word

To check if a string starts with some specific word(s) we use StartsWith method. Return type of this method is Boolean. string Fruit = "Apple" ; if (Fruit.StartsWith( "A" )) {     Console .WriteLine( "Fruit starts with A" ); } Output Fruit starts with A

Replace contents/html of an html element

To replace existing contents/html of an html element using jquery we use html method. This method replaces existing contents/html of element with new html. < input type ="button" value ="Change Html" onclick =" ChangeHtml() " /> < div id ="myDiv" class ="myClass">     This is a div </ div > < script type ="text/javascript">     function ChangeHtml() {         $( "#myDiv" ).html( "<h1>This is new html</h1>" );     }     </ script >

Split string into multiple strings in C#

To split a string into multiple strings we use Split method. This method has 6 overloads. We will use the Character separator overload to split string using single character. Split method returns array of strings. string StrToSplit = "Sunday is funday" ; string [] SplittedString = StrToSplit.Split( ' ' ); foreach ( string str in SplittedString) {     Console .WriteLine(str); } Output Sunday Is funday

Append html/text in an html element

To append some text or an html element within an html element we use append method. This will append the given text/html to end of the element in which we are appending. < input type ="button" value ="Append To Div" onclick =" Append() " /> < div id ="myDiv" class ="myClass">     This is a div </ div >             < script type ="text/javascript">     function Append() {         $( "#myDiv" ).append( "<h1>This text is appended by jquery</h1>" );     }     </ script >

How to check if string is null or empty

To check if string is null or empty we use IsNullOrEmpty method.This method returns Boolean value. If string is null or is empty it will return true otherwise false . string Name = "" ; if ( string .IsNullOrEmpty(Name)) {     Console .WriteLine( "Name is null or empty" ); } Output Name is null or empty

Show hide html element using jquery

To show a hidden element we use show method. To hide an element we use hide method. < input type ="button" value ="Hide Div" onclick =" HideDiv () " /> < input type ="button" value ="Show Div" onclick =" ShowDiv () " /> < div id ="myDiv" class ="myClass">     This is a div </ div > < script type ="text/javascript">     function ShowDiv() {         $( "#myDiv" ).show();     }     function HideDiv() {         $( "#myDiv" ).hide();     } </ script >