Solved

javascript / jquery selector for getting control's attribute

Posted on 2011-09-22
3
267 Views
Last Modified: 2012-05-12
I am running Visual Studio 2010.  The problem is I don't know how to get the value of a control's' custom attribute.

Here is my HTML:

<div class="row">
                    <asp:RadioButton runat="server" class="petsRadioButton" ID="rbLiveStock" Text=" Livestock  -  Please Describe: " breedCd="LIVESTOCK" />
                    <asp:TextBox runat="server" ID="txtLivestockDesc" onchange="txtLivestockDesc_Updated(this.value)" style="width:100px; margin-top: 2px;"  />
</div>

I am trying to get the value of breedCd attribute of the radio button above.  
Here is the relevant cut from the page generated by VS:

<div class="row">
                    <span class="petsRadioButton" breedCd="LIVESTOCK"><input id="ctl00_Main_rbLiveStock" type="radio" name="ctl00$Main$rbLiveStock" value="rbLiveStock" /><label for="ctl00_Main_rbLiveStock"> Livestock  -  Please Describe: </label></span>
                    <input name="ctl00$Main$txtLivestockDesc" type="text" id="ctl00_Main_txtLivestockDesc" onchange="txtLivestockDesc_Updated(this.value)" style="width:100px; margin-top: 2px;" />
                       
</div>

Here is my function:

function txtLivestockDesc_Updated(text) {
        debugger
        var radioButtonChecked = false;
        var rbLiveStock = 'ctl00_Main_rbLiveStock';
 
        if (text != null && text.trim() != "") {
            $(rbLiveStock).attr('checked', 'checked');
            radioButtonChecked = true;
        }
        else {
            $(rbLiveStock).attr('checked') ? radioButtonChecked = true : radioButtonChecked = false;
        }
 
        var breedCd1 = $(rbLiveStock).parent().attr('breedCd'); //undefined
        //var breedCd2 = rbLiveStock.parent.getAttribute('breedCd'); // parent is null or not an object
        var breedCd3 = $(rbLiveStock.parent).attr('breedCd'); // undefined
 
        updateAnimalExposureInfo($(rbLiveStock.parent).attr('breedCd'), radioButtonChecked, text);
    }

Please help me get the value of the breedCd attribute.
Thank you.




in the txtLivestockDesc_Updated function:

    function txtLivestockDesc_Updated(text) {
        debugger
        var radioButtonChecked = false;
        var rbLiveStock = '<%=rbLiveStock.ClientID%>';

        if (text != null && text.trim() != "") {
            $(rbLiveStock).attr('checked', 'checked');
            radioButtonChecked = true;
        }
        else {
            $(rbLiveStock).attr('checked') ? radioButtonChecked = true : radioButtonChecked = false;
        }

        var breedCd = $(rbLiveStock.parent).attr('breedCd');
}
0
Comment
Question by:Mare22
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 36582125
var breedCd1 = $("#" + rbLiveStock).parent().attr('breedCd');

0
 

Author Closing Comment

by:Mare22
ID: 36582182
Of course! Thank you leakim971 .
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36582183
Check this : http://jsfiddle.net/mSV4T/

var rbLiveStock = '#<%=rbLiveStock.ClientID%>';
function txtLivestockDesc_Updated(text) {
        var radioButtonChecked = false;
        var rbLiveStock = '#<%=rbLiveStock.ClientID%>';

        if (text != null && text.trim() != "") {
            $(rbLiveStock).attr('checked', true);
            radioButtonChecked = true;
        }
        else {
            $(rbLiveStock).is(':checked') ? (radioButtonChecked = true) : (radioButtonChecked = false);
        }

        var breedCd = $(rbLiveStock).parent().attr('breedCd');
}

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

740 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