Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get value of custom attribute

I have two radio buttons. I would like to be able to get the value of the custom attribute "xmlvalue" of the checked radio button.

I have tried with the following script:

var userType = $("input[name=ctrl_CustomerType]:checked", this).attr('xmlvalue');

Markup:

<input type="radio" name="ctrl_CustomerType" id="ctrl_CustomerType_1" xmltag="CustomerType" xmlvalue="existingCustomer" checked="checked"> Yes
<br />
<input type="radio" name="ctrl_CustomerType" id="ctrl_CustomerType_2" xmltag="CustomerType" xmlvalue="newCustomer"> No

Fiddle here

-- But I keep getting "Undefined".

Any ideas?

like image 251
Meek Avatar asked Jun 04 '13 11:06

Meek


People also ask

How do I get custom attribute values?

Retrieving a custom attribute is a simple process. First, declare an instance of the attribute you want to retrieve. Then, use the Attribute. GetCustomAttribute method to initialize the new attribute to the value of the attribute you want to retrieve.

How can get custom data attribute value in jQuery?

To retrieve a data-* attribute value as an unconverted string, use the attr() method. Since jQuery 1.6, dashes in data-* attribute names have been processed in alignment with the HTML dataset API. $( "div" ).

How to access data attributes in React?

Use the target. dataset property to access data attributes from the event object in React. The dataset property provides read and write access to the custom data attributes of the element. The property returns a Map of strings which can be converted to an object.


2 Answers

Remove the context of your selector:

http://jsfiddle.net/NrQek/1/

 var userType = $("input[name=ctrl_CustomerType]:checked").attr('xmlvalue');
        alert("xmlvalue is: " + userType);
like image 137
A. Wolff Avatar answered Oct 29 '22 04:10

A. Wolff


Your selector is wrong.

The input element is not children of a element where you are clicking, so you cannot pass this as a context to the selector

var userType = $("input[name=ctrl_CustomerType]:checked").attr('xmlvalue');

Demo: Fiddle

like image 32
Arun P Johny Avatar answered Oct 29 '22 05:10

Arun P Johny