Altaf Khatri
Altaf Khatri



RSS feed for http://www.AltafKhatri.com website. Twitter account of http://www.AltafKhatri.com website.
∗Recently Added - How to take care of Betta fighter fish and increase life span?  10/30/2013


Find me on web

 LinkedIn

 Facebook

 Twitter

Jquery - How to find element with special characters?

While using Jquery with ASP.NET MVC, we get the elements name having period/dot/. and left parenthesis([) and a right parenthesis(]). This article describes how to find element by name or by id for the elements with these special characters.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

 

<html>

<head>

    <title>Find Element by Name and Id - containing special characters</title>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>

 

    <script type="text/javascript">

        function btn_Click(myId) {

            alert("Button Id: " + myId);

           

            var btnName = myId;

            btnName = btnName.replace(/\./g, "\\.");

            btnName = btnName.replace(/\[/g, "\\[");

            btnName = btnName.replace(/\]/g, "\\]");

            alert("Jquery suitable Button Id: " + btnName);

           

            var btn = $("[id='" + btnName + "']").attr("value"); //Method 1

            //var btn = $("#" + btnName).attr("value"); //Method 2

           

            alert("Button value: " + btn);

        }

 

        function a_Click(myName) {

            alert("Anchor Name: " + myName);

            var btnName = myName;

                       

            btnName = btnName.replace(/\./g, "\\.");

            btnName = btnName.replace(/\[/g, "\\[");

            btnName = btnName.replace(/\]/g, "\\]");

            alert("Jquery suitable Anchor Name: " + btnName);

 

            var btn = $("[name='" + btnName + "']").html(); //var btn = $("[name='" + btnName + "']").attr("href");

            alert("Anchor text: " + btn);

        }

    </script>

 

</head>

<body>

    <a name="a[0].[1].pk" id="a_0_1_pk" href="#" onclick="a_Click(this.name);return false;">Click Me - Altaf Khatri</a>

    <input id="btn[0].[1].[222].pk" type="button" value="Click Me" onclick="btn_Click(this.id);"/>

</body>

</html>

 





SiteMap  |  Contact Me