Solved

JQuery accessing attributes from click event

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
The viewer will learn how to count occurrences of each item in an array.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

707 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

14 Experts available now in Live!

Get 1:1 Help Now