Solved

JQuery accessing attributes from click event

Posted on 2011-09-18
4
281 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Logon script fails 23 45
how can i get the string decoded ? 2 28
xjs: how to get sum of value in for loop 2 18
javascript - insert into js doc help 2 19
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to dynamically set the form action using jQuery.

792 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