Showing posts with label jQuery. Show all posts
Showing posts with label jQuery. Show all posts

jQuery Tabs - UI

jquery tab
jQuery UI libaries provied good controls. jQuery UI contains tab control that is used to show tabed formatted data. We will see example how to show jquery tabs.
Before starting implementation we need to download jQuery libraries.http://jqueryui.com/download.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="development-bundle/jquery-1.7.1.js" type="text/javascript"></script>
<script src="js/jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="js/jquery-ui-1.8.18.custom.min.js" type="text/javascript"></script>
<script src="development-bundle/ui/jquery.ui.widget.js" type="text/javascript"></script>
<script src="development-bundle/ui/jquery.ui.tabs.js" type="text/javascript"></script>

<link href="Styles/jquery-ui-1.8.18.custom.css" rel="stylesheet" type="text/css" />
<title></title>

<script type="text/javascript">
$(function () {
// Tabs
$('#tabs').tabs();
});

</script>
</head>
<body>
<form id="form1" runat="server">
<div id="tabs">

<ul>
<li><a href="#tabs-1">Tab1</a></li>
<li><a href="#tabs-2">Tab2</a></li>
<li><a href="#tabs-3">Tab3</a></li>
</ul>
<div id="tabs-1">Tab1 Contents.</div>
<div id="tabs-2">Tab2 Contents.</div>
<div id="tabs-3">Tab3 Contents.</div>

</div>
</form>
</body>
</html>

Show jQuery popup dialog

jquery dialog
In web applications we need to show popup dialog to show errors or dialog to show message or to show confirmation dailog with ok and cancel button. jQuery UI library provides easy way to show dialog as model or simple dailog. For that we need to add ui js libraries in application.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script src="development-bundle/jquery-1.7.1.js" type="text/javascript"></script>
    <script src="js/jquery-1.7.1.min.js" type="text/javascript"></script>
    <script src="js/jquery-ui-1.8.18.custom.min.js" type="text/javascript"></script>
    <script src="development-bundle/ui/jquery.ui.widget.js" type="text/javascript"></script>
    <script src="development-bundle/ui/jquery.ui.dialog.js" type="text/javascript"></script>
    <link href="Styles/jquery-ui-1.8.18.custom.css" rel="stylesheet" type="text/css" />
    <title>jQuery Popup</title>
    <script type="text/javascript">
        $(function () {
            // Dialog  
            $('#dialog').dialog({
                autoOpen: false,
                width: 600,
                buttons: {
                    "Ok": function () {
                        $(this).dialog("close");
                    },
                    "Cancel": function () {
                        $(this).dialog("close");
                    }
                }
            });
            // Dialog Link
            $('#btnDelete').click(function () {
                $('#dialog').dialog('open');
                return false;
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div id="dialog" title="Dialog Title">
        <p>
            Do you want to delete record?</p>
    </div>
    <input type="button" id="btnDelete" value="Delete" />
    </form>
</body>
</html>

Assign value using jQuery

assign values in jQuery
In asp.net applications we need to assign value in javascript or jQuery to the controls like label or hiddenfield. We will see sample code that assigns value to lable and hidden field using jQuery.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<title>assign value in jQuery</title>
<script type="text/javascript">
$(document).ready(function () {

});

function AssignValue() {
$("#message").html('New Value assigned');
$("#hiddenValue").val('New Value assigned');
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>
<label id="message">
Some Value</label>
<input type="submit" name="btnsubmit" id="btnsubmit" value="LogIn" onclick="return AssignValue();" />
<input type="hidden" id="hiddenValue" />
</p>
</div>
</form>
</body>
</html>

In above example if we want to assign value to hidden field and get hidden field value in code behind we need to remove "return false;" statement from jQuery code so that page will get postback.

jQuery validate form empty fields

validate empty fields using jQuery
In Web forms we need to validate fields on the form to check field should not be empty. For example for login form we need to validate textboxes for username and password to check textbox not empty.

Example to validate textbox not empty using jQuery

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<title>Validate empty field using jQuery</title>

<script type="text/javascript">
$(document).ready(function () {

});

function Validate() {
var username = $("#username").val();
var password = $("#pwd").val();

if (username == "" && password == "") {
alert("Enter username and password");
return false;
}

if (username == "") {
alert("Enter username");
return false;
}

if (password == "") {
alert("Enter password");
return false;
}

}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<p> 
<label for="userName">Username</label>
<input type="text" name="log" id="username" value="" size="50" tabindex="10" />
</p> 
<p> 
<label for="pwd">Password</label> 
<input type="password" name="pwd" id="pwd" value="" size="50" tabindex="20" />
</p> 
<p> 
<input type="submit"  name="btnsubmit" id="btnsubmit" value="LogIn" onclick="return Validate();"/>
</p> 
</div>
</form>
</body>
</html>

Get RadioButtonList value in jQuery

Get radiobuttonlist value using jquery
In asp.net while using radiobuttonlist we need to find selected radio buttons value in jQuery. We see example how to get value of selected radio button in jQuery.

Example to get radio button value in jQuery

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<title>Get radiobutton value in jQuery</title>

<script type="text/javascript">
$(document).ready(function () {
$('#radiolist-container input').click(function () {
alert($(this).val());
}); 

});
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="radiolist-container">
<p><label>Get radiobutton value in jQuery</label> 
<asp:RadioButtonList ID="radioOption" runat="server"> 
<asp:ListItem Text="ASP.NET" Value="ASP.NET"></asp:ListItem> 
<asp:ListItem Text="C#" Value="C#"></asp:ListItem> 
</asp:RadioButtonList></p>
</div>
</form>
</body>
</html>

Detect if JQuery dialog is open

detect if dialog is open
when using a JQuery dialog, in some cases we neeed to check if dialog is open or not. In below example we will see how do to detect if a JQuery dialog box is open or not

jQuery example that check's if dailog is open [jQuery]

<html>
<head><title></title>
<script src="jquery-1.4.1.js" ></script>
<script src="jquery-1.4.1.min.js" ></script>

<style type="text/css"> 
  .info {
   position: absolute;
   z-index: 200;
   display: none;
   text-align: center;
   background: gray;
   border: 2px solid silver;
 }
</style>
<script type="text/javascript"> 
//<![CDATA[
     $(document).ready(function () {
         initializeDialog();
         OpenDailog();
     });

     function initializeDialog() {
         $('#dialog').dialog({
             width: 650,
             modal: true,
             title: 'Dailog Title',
             buttons: { Close: function() { $(this).dialog('close'); } }
         });
     }

     function OpenDailog() {
         var $dlg = $('#dialog');
         //Here We check if dailog is open or not
         if ($dlg.dialog('isOpen') == true) {
             //Perform some operations;
         }
         $dlg.append('<p>Dailog Contents</p>');
         $dlg.dialog('open');
         $dlg.dialog('option', 'title', 'dailog Title'); 
     }
//]]>
</script>
</head>
<body>
<form action="">
<div id="dailog" class="info">
</div>
</form>
</body>
</html>


In above code if ($dlg.dialog('isOpen') == true) is imporant code that check's if jQuery dailog is open or not. You need to explicitly check == true otherwise $dlg.dialog('isOpen') will just return object only.

Click event in jQuery

jQuery mouse click event
In web application we need capture mouse click event in jQuery. To perform some clientside operations in asp.net we can easly capture mouse click event in jQuery.

jQuery mouse click event happens when we click mouse button on some spicific element on page. We can use click event to show some information box, or we can show hide div on click event of mouse...etc
Here we will look at very simple exaple of how to show hide div when we click on link

Sample code for click event in jQuery [ASP.NET]


<html>
<head><title></title>
  <script src="jquery-1.4.1.js" ></script>
  <script src="jquery-1.4.1.min.js" ></script>
    
 <style type="text/css"> 
  .info {
   position: absolute;
   z-index: 200;
   display: none;
   text-align: center;
   background: gray;
   border: 2px solid silver;
 }
</style>
 <script type="text/javascript"> 
//<![CDATA[
  $(document).ready(function(){
   $("a").click(function(){
   $(".info").show();
  });
//]]>
</script>
</head>
<body>
 <form>
 <p>
   Click on this link <a id="aspnet" href="#" >jQuery mouse clickr</a>.
 </p>
 <div class="info">
   You will see some infomation when you <b>mouse click</b> on link. 
 </div>
</form>
</body>
</html>

Mouseover event in jQuery

mouseover jquery
In web application often we need mouseover in jquery. jQuery mouseover event triggers when we take mouse over perticular DOM element on page.

We can use mouseover to show custom tooltip on link or to show mouseover context menu...etc.jQuery provides lot's of mouse event's in order to deal with clientside operation in asp.net.
Here we will look at very simple exaple of how to show hide div when we mouseover on link.

Sample to show how mouseover in jQuery works [ASP.NET]


<html>
<head><title></title>
    <script src="jquery-1.4.1.js" ></script>
    <script src="jquery-1.4.1.min.js" ></script>
    
    <style type="text/css"> 
 .info {
  position: absolute;
  z-index: 200;
  display: none;
         text-align: center;
  background: #fff;
     border: 1px solid silver;
 }
</style>
 <script type="text/javascript"> 
//<![CDATA[
        $(document).ready(function(){
            $("a").mouseover(function(){
                $(".info").show();
           });

           $("a").mouseout(function () {
               $(".info").hide();
           });
        });
//]]>
</script>
</head>
<body>
    <form>
<p>
Mouseover on this link <a id="aspnet" href="#" >jQuery mouseover</a>.
    </p>
<div class="info">
You will see some infomation when you <b>mouseover</b> on link.
And infomation will hide when you <b>mouseout</b> from link.
     </div>
</form>
</body>
</html>

Get checked checkboxes in jQuery

get checked checkboxes in jquery


In this post we are going to see how to get all the checked checkboxes using jQuery. Some time in asp.net applications we need to do operations at client side on controls like checkboxes, dropdown list, buttons…etc. Here we will see how to access checkboxes at client side using jQuery and how to get checked checkboxes. To get the checked checkboxes, jQuery provides selector :checked.

Sample code to get all checked checkboxes[jQuery – ASP.Net]

<input value="1" checked type="checkbox" name="numbers" />
<input value="2" type="checkbox" name="numbers" />
<input value="3" checked type="checkbox" name="numbers" />

Now to get only the checked checkboxes we will use below line of code that will store all the checked checkboxes as an array.


$checkedCheckboxes = $("input:checkbox[name=numbers]:checked");;

We can iterate though the checked checkbox array using .each and perform some operations or can get or set values to checkboxes.


var selectedValues="";
$checkedCheckboxes.each(function () {
selectedValues +=  $(this).val() +",";
});
);

Let's put all above code togather

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<title></title>
<script type="text/javascript">
$(document).ready(function () {
var selectedValues="";
$checkedCheckboxes = $("input:checkbox[name=numbers]:checked");
$checkedCheckboxes.each(function () {
selectedValues +=  $(this).val() +",";
});
alert(selectedValues);
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input value="1" checked type="checkbox" name="numbers" />
<input value="2" type="checkbox" name="numbers" />
<input value="3" checked type="checkbox" name="numbers" />
</div>
</form>
</body>
</html>

//Output
//1,3

using jquery

jquery attribute selectors

How to Use jQuery Selectors?


jQuery has power of  minimizing lines of code. jQuery would be the powerful tool for the DOM. jQuery is easy to select elements using selector engine, it’s easy syntax, which is based on the same syntax you would use to select elements in CSS by borrowing from CSS 1-3. jQuery  selects or finds element on page by Element Id, it is jQuery option to getElementById. By using get element by id selector jQuery uses elements Id. Other selector jQuery uses is Get element by class. In this jQuery selector class name of the  element is used to access the elements on page. jQuery also get element by attribute. Also by using hierarchy jQurey selects element on page. jQuery has filters that are also useful to get elements.We will see some samples of different types of selectors.

Element by ID         jQuery getElementByID     


In the example below we will see a selection of an element by id.It is same as java scripts getElementById(). Using the id of element jQuery gets element on the page.It has every easy syntax.

<html>
<head>   
<title>Get Element by ID</title>
<script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.js"></script> 
<script type="text/javascript">     
//get element by it's ID   
alert($("#message").text()); // Hello jQuery  
</script>
</head> 
<body>    
<div id="message">Hello jQuery</div>  
</body>  
</html>




 



jQuery uses the native JavaScript function getElementById(), for id selectors. The fastest way to select an element is to reference it by its id selector.
It’s also important that your id should not contain the special characters like:
#;&,.+*~':"!^$[]()=>|/
The characters above if not properly escaped ay confuse the jQuery selector engine as it is a different type of selector. You can use escape character way like below to handle special characters.

$(“#some:id”) -> $(“#some\\:id”)

 

Get Elements by its class


In below example we will select all elements on the page that have the specified class name. jQuery uses the class of element and get the element from the page.
jQuery GetElement By Class

<html>  
<head>  
<title>Get Element by class selector</title>  
<script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.js"></script>  
<script type="text/javascript">  
//Get  the element by it's classname   
alert($(".message ").text()); // HellojQuery  
</script>  
</head>  
<body>  
<p id="hello">  
<span class="message">Hello</span> <span class="message">jQuery</span>  
</p>  
</body>  
</html>  



In above example, selector matched both the span elements that have class= “message” and combined contents into one result ‘HellojQuery’.

jQuery will use the native JavaScript function GetElementsByClassName().

 

Get Elements by Attribute


Below example shows how to select elements that have an attribute with some specific value.

<html>  
<head>  
<title>Get Element by its attribute</title>  
<script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.js"></script>  
<script type="text/javascript">  
//get  elements by its attribute   
alert($("input[name= name]").val()); // johan   
</script>  
</head>  
<body>  
<form>  
<p> Name: <input type="text" name="name"  value="Johan" />  </p>
<p> Phone: <input type="text" name="phone" value="0000000" /> </p>
</form>  
</body>  
</html>  



Here selector finds all matching ‘input’ elements then the element that have an attribute of ‘name’ and that also have the value ‘name’.

 

Get Elements by Hierarchy


Descendent Selector (“parent child”) – This will selects all descendent child elements of the parent including element which may a child, grandchild, great-grandchild, and so on, of that element.

Example:

<p><a href="http://google.com">Google</a></p>  
<div>  
<p> <a href="http://Yahoo.com">Yahoo!</a></p>  
<p><a href="http://gmail.com">Gmail</a></p>  
</div>  
<ul>
<li><a href="http://jquerymobile.com">jQuery Mobile</a></li>
</ul>
<div>
<ul>
<li><a href="http://jquery.com">jQuery</a></li>
<li><a href="http://jqueryui.com">jQuery UI</a></li>
</ul>
</div> 



When we write jQuery like:

$("div p");   



It will give us below result:

<p> <a href="http://Yahoo.com">Yahoo!</a></p>  
<p><a href="http://gmail.com">Gmail</a></p>  



And if we write like below code

$("div ul"); 



then it’s result will be like:

<ul>
<li><a href="http://jquery.com">jQuery</a></li>
<li><a href="http://jqueryui.com">jQuery UI</a></li>
</ul>



Child Selector (“parent > child”) – This may be used when you need to selects all direct child elements of the parent.
Example:

<p><a href="http://google.com">Google</a></p>  
<div>  
<p> <a href="http://Yahoo.com">Yahoo!</a></p>  
<p><a href="http://gmail.com">Gmail</a></p>  
</div>  
<ul>
<li><a href="http://jquerymobile.com">jQuery Mobile</a></li>
</ul>
<div>
<ul>
<li><a href="http://jquery.com">jQuery</a></li>
<li><a href="http://jqueryui.com">jQuery UI</a></li>
</ul>
</div> 



When we write jQuery like:

$("p > a");   



This will give us all the <a> tags:

<a href="http://google.com">Google</a>
<a href="http://Yahoo.com">Yahoo!</a></
<a href="http://gmail.com">Gmail</a> 



And below code will be resulted as:

$("li > a"); 



Result of above code will be

<a href="http://jquerymobile.com">jQuery Mobile</a>
<a href="http://jquery.com">jQuery</a>
<a href="http://jqueryui.com">jQuery UI</a> 



Get Elements by Filters

<!DOCTYPE html>
<html>
<head>
<title>Select Element by Selector Filter</title>
</head>
<body>
jQuery project sites:
<ul>
<li><a href="http://jquery.com">jQuery</a></li>
<li><a href="http://jqueryui.com">jQuery UI</a></li>
<li><a href="http://sizzlejs.com">Sizzle</a></li>
<li><a href="http://jquery.org">jQuery Project</a></li>
</ul>
<script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.js"></script>
<script type="text/javascript">
//Select the element by attribute
alert($("a:first").attr("href")); // http://jquery.com
</script>
</body>
</html> 



There are three types of filter selectors that you can used to get elements: numerical or positioned filters, CSS filters and Pseudo filters

 


Numerical or positioned filters:


:first< matched first Gives –>

:last - Gives last matched element

:first-child – Gives the first child element

:last-child - Gives the last child element

:only-child - Gives all element that have no siblings

:nth-child(n) - Gives the nth child element

:nth-child(even|odd) – Gives the matched  even or odd children elements

:nth-child(xn+y) - Gives the nth matched children elements based on some formula.

:even - Gives even children elements

:odd - Gives odd children elements

:eq(n) - Gives nth position child element

:gt(n) - gives the children element those are greater than the nth position

:lt(n) - gives the children element those are less than the nth position

Traditional CSS filters:

:checked – It is basically used with checkboxes or radio buttons. It will return all checked elements.

:disabled – This will return all disabled elements

:enabled – This will give us all enabled elements

 

jQuery Pseudo filters:


:animated – Gives all elements with animations.

:button – Returns all button elements

:checkbox - Returns all checkbox elements

:contains(string) - Returns the elements those contain given string

Example:  $(“div:contains(Google)”)

:file – Return all file input elements

:has(selector) –Returns elements that have at least one of the specified matched selector.

:header - Returns header elements.

:hidden – Returns all hidden elements

:image - Returns image input elements

:input - Returns form elements

:parent - Returns elements that have children, including text, which are not empty

:password - Returns elements of type password

:radio - Returns radio elements

:reset - Returns reset buttons

:selected - Returns option elements in selected state

:submit - Returns elements of type submit

:text - Returns elements of type text

:visible - Returns elements that are visible