Solved

JQuery accessing attributes from click event

Posted on 2011-09-18
4
277 Views
Last Modified: 2012-05-12
I am working with JQuery 1.4 and Visual Studio 2010.

I have a radio button:

<div>
     <asp:RadioButton runat="server" ID="C" Text=" ..." code="cd" />
</div>

and a click event handler:

$(document).ready(function () {
 var rb = '#<%=myID.ClientID%>';  
 $(rb).click(function () {
        var code = $(this).attr("code");
   });
});

I need to get the button's attributes, but they come in undefined.  How can this be fixed?
Thank you.
0
Comment
Question by:Mare22
  • 2
  • 2
4 Comments
 

Author Comment

by:Mare22
ID: 36557675
well, actually, I am running the code from my previous question:

        $(":radio", ".petsRadioButton").each(function () {
                $("form").append("<input type='radio' name='" + $(this).attr("name") + "' style='display:none' value='0' />");
        })
        $(":radio:visible", ".petsRadioButton").click(function () {
            var breedCode = $(this).attr("breedCode");
            var txt = $(this).attr("Text");
        debugger
            var $bro = $(":radio[name='" + $(this).attr("name") + "']").not(this);
            if ($bro.val() == "0") {
                $bro.val("1");
                $bro.attr("checked", false);
            }
            else {
                $bro.val("0");
                $bro.trigger("click");
            }

            var checked =  $(this).attr("checked");
            if (checked == undefined) {
                checked = false;
            }
            else {
                checked = true;
            }

        });

breedCode and txt are undefined.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36557701
you should be able to use : $(this).next("label").text()

Check this page, the html is the one generated by .net : http://jsfiddle.net/EAvsh/5/
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 36557703
            $(':radio:visible').click(function () {
                var $bro = $(":radio[name='" + $(this).attr("name") + "']").not(this);
                if ($bro.val() == "0") {
                    $bro.val("1");
                    $bro.attr("checked", false);
                }
                else {
                    $bro.val("0");
                    $bro.trigger("click");
                }
                var text = $(this).next("label").text();
                alert(text);
            });

Open in new window

0
 

Author Closing Comment

by:Mare22
ID: 36571670
I've actually used $(this).parent, but your comment helped.
Thank you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now