Saturday, September 22, 2012

Asp.Net jQuery set href Target Attribute


The jQuery library provides various options to work with href links, in this post Asp.Net jQuery set href Target Attribute, we shall see on how to set the Target Attribute of a Hyperlink control using jQuery.

We can set the Target Attribute of a href control using jQuery as follows.

$('#lnkGoogle').attr('target', '_self');


Here is a full example

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Links (a href)</title>
    <link
      type="text/css"
      href="Stylesheet.css"
      rel="Stylesheet" />
    <script
      type="text/javascript"
      src="JavaScript/jquery-1.7.2.js"></script>
    <script
      type="text/javascript"
      language="javascript">
        $(document).ready(function() {
            $('#cmdSet').click(function(event) {
                event.preventDefault();
                $('#lnkGoogle').attr('target', '_self');
            });       
        });
    </script>       
</head>
<body>
    <form id="frmLinks" runat="server">
        <b>Links (a Href)</b><br />
        <a id="lnkGoogle"
            href="http://www.google.com"
            target="_blank">Google</a>
        <br />
        <a id="lnkYahoo"               href="http://www.yahoo.com">Yahoo</a><br />
        <a id="lnkFacebook" href="http://www.facebook.com">Facebook</a><br />
        <br /><br />
        <b>Hyperlinks (asp:HyperLink)</b><br />
        <asp:HyperLink
            ID="lnkMicrosoft"
            runat="server"
            Text="Microsoft"
            NavigateUrl="http://www.microsoft.com/">
        </asp:HyperLink><br />
        <asp:HyperLink
            ID="lnkMSDN"
            runat="server"
            Text="MSDN"
            NavigateUrl="http://msdn.microsoft.com/en-US/">
        </asp:HyperLink> 
        <br /><br />
        <asp:Button
            ID="cmdSet"
            runat="server"
            Text="Set"
            Width="100"/>           
    </form>
</body>
</html>

Run the application, click on the Set button, the Target Attribute of the Hyperlink control will get set to _self.

Search Flipkart Products:
Flipkart.com

No comments: