Image click event in jquery

Image click event in jQuery

In this post we are going to see how to get click event of image. This can be done by using either id jquery selector or css selector. We will see both the examples to get image click in jquery.

Image click event using id selector [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 Image click event</title>
<script type="text/javascript">
$(document).ready(function () {
$("#tulips").click(function () {
alert("Image click in jquery");
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<img src="Image/Tulips.jpg"  runat="server" id="tulips" alt="image click in jquery"/>
</div>
</form>
</body>
</html>

Image click event using css selector [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 Image click event</title>
<script type="text/javascript">
$(document).ready(function () {
$(".imagecss").click(function () {
alert("Image click in jquery");
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<img src="Image/Tulips.jpg"  runat="server" id="tulips" alt="image click in jquery" class="imagecss"/>
</div>
</form>
</body>
</html>

Convert Date to culture specific format

datetime culture
In C# we need to show date format as per culture.To convert datetime in culture specific format we need to use CultureInfo class in .ToString constructor with culture code like for US culture pass "en-US". We will see here few examples to format date as per culture.

Code to format date in US culture [C#]

using System;
 public class StringToDateTime
 {
   public static void Main()
   {
     var myDateTimeValue = "2012-01-16 15:02:26";
     DateTime myDateTime = DateTime.Parse(myDateTimeValue);
     var dateingddmmyy = myDateTime.ToString(new CultureInfo("en-US"));
     Console.Write(dateingddmmyy);
   }
 }

Code to convert date in sanskrit Indian culture [C#]

using System;
 public class StringToDateTime
 {
   public static void Main()
   {
     var myDateTimeValue = "2012-01-16 15:02:26";
     DateTime myDateTime = DateTime.Parse(myDateTimeValue);
     var dateingddmmyy = myDateTime.ToString(new CultureInfo("sa-IN"));
     Console.Write(dateingddmmyy);
   }
 }


Code to convert date in Swedish culture [C#]

using System;
 public class StringToDateTime
 {
   public static void Main()
   {
     var myDateTimeValue = "2012-01-16 15:02:26";
     DateTime myDateTime = DateTime.Parse(myDateTimeValue);
     var dateingddmmyy = myDateTime.ToString(new CultureInfo("sv-SE"));
     Console.Write(dateingddmmyy);
   }
 }

Convert date to ""dd/mm/yyyy" format C#

format datetime in dd/mm/yyyy

In c# we need to convert datetime in different formats.In this post we will see how to get datetime in "dd/mm/yyyy" format.To parse datetime in "dd/mm/yyyy" format we need to use string formatter "dd/mm/yyyy" in .ToString() constructor.

Code to get datetime in dd/mm/yyyy format [C#]

using System;
 public class StringToDateTime
 {
   public static void Main()
   {
     var myDateTimeValue = "2012-01-16 15:02:26";
     DateTime myDateTime = DateTime.Parse(myDateTimeValue);
     var dateingddmmyy = myDateTime.ToString("dd/mm/yyyy");
     Console.Write(dateingddmmyy);
   }
 }
 

Get dropdown selected item onchange in jQuery

get dropdown list selected item onchange in jquery
When using dropdown list in asp.net application, we need to get value of selected item on onchange event of dropdown. In this post we will see example of how to get value of selected item using onchange event of dropdown.

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" 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 dropdown value</title>
<script type="text/javascript">
$(document).ready(function () {
$("#topics").change (function () {
var text = $("#topics option:selected").text();
var value = $("#topics option:selected").val();
alert("Selected text=" + text + " Selected value= " + value);
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="topics" runat="server">
<asp:ListItem Text="C#" Value="1"></asp:ListItem>
<asp:ListItem Text="jQuery" Value="2"></asp:ListItem>
<asp:ListItem Text="ASP.NET" Value="3"></asp:ListItem>
</asp:DropDownList>
</div>
</form>
</body>
</html>
Output

Get dropdownlist selected value in jQuery

get dropdownlist selected value in jquery
In web application we need to get dropdown value at client side. We will see the example of how to get dropdown value in jQuery.

Code to get dropdown value in jQuery


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"> type="text/javascript"> </script>
  <title>Get dropdown value</title>
  <script type="text/javascript">
    $(document).ready(function () {
      $("#go").click(function () {
        var text = $("#numbers option:selected").text();
        var value = $("#numbers option:selected").val();
        alert("Selected text=" + text + " Selected value= " + value);
      });
    });
  </script>
</head>
<body>
  <form id="form1" runat="server">
  <div>
    <asp:DropDownList ID="numbers" runat="server">
      <asp:ListItem Text="1" Value="1"></asp:ListItem>
      <asp:ListItem Text="2" Value="2"></asp:ListItem>
      <asp:ListItem Text="3" Value="3"></asp:ListItem>
    </asp:DropDownList>
  </div>
  <input type="button" id="go" value="Go" />
  </form>
</body>
</html>

Get radio button value in jQuery

Get radio button value in jquery
When using radio buttons in web application we need to get value of radio buttons at clientside. We can use jQuery to get radio button value in jQuery.

Code that shows how 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 radio button value</title>
<script type="text/javascript">
$(document).ready(function () {
$("#submit").click(function () {
var number = $("input[@name=numbers]:checked").val();
alert(number);
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="radio" name="numbers" value="0" checked="checked" style="display:none" /> 
<input type="radio" name="numbers" value="1" /> 
<input type="radio" name="numbers" value="2" /> 
<input type="radio" name="numbers" value="3" /> 
<input id="submit" type="button" value="Submit"/>
</div>
</form>
</body>
</html>
Output

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>

Confirmationbox in javascript

show confirmationbox in javascript
In asp.net we need to show javascript confirmation box before doing some action.For example if we want to delete some record on the form, and we need users confirmation before deletion happens.

Sample code to show confirmation box in Javascript

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function Confirmation() {
if (confirm("Do you want to delete record?") == true)
return true;
else
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" value="Submit" onclick="return Confirmation();" />
</div>
</form>
</body>
</html>

Disable button jQuery

Disabling button after clicking on it is bit easy in jQuery.We can access button element of the page in jQuery and disable button using jquery. For that we can get button element using Id attribute selector or using class attribute selector.For example We may need to disable submit button after submitting form by clicking on it.

Sample code to disable submit button[jQuery + asp.net]

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" 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 () {
});

function Disable(btn) {
$(btn).attr("disabled", "true");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" value="Submit" onclick="return Disable(this);" />
</div>
</form>
</body>
</html>