0

i gave one dropdown and it has one custom attributed i added called ShipTo. now my dropdown html looks like

<select id="ddlCountry" runat="server" style="width: 300px; font-size: 9pt; float:left; margin-top: 3px;">
<option shipto=''  value="">Select your location</option>
<option shipto='GBR'  value="gb_en_home">United Kingdom - English</option>
<option shipto='USA'  value="us_en_home">United States - English</option>
</select>    

this way i try to read custom attribute value

var ShipTo = $("#ddlCountry").attr("shipto");
alert(ShipTo );

but the above code does not work. so can anyone guide me how to achieve it. thanks

EDIT

i figured out how to read that custom attribute data

var ShipTo = $("#ddlCountry").find('option:selected').attr("shipto"); 

the above code works fine.

4 Answers 4

3

You are trying to get the attribute from select, you need to get it from the option like this

$('#ddlCountry option:selected').attr('shipto');

Use :selected to get the selected option.

DEMO

Sign up to request clarification or add additional context in comments.

Comments

2

The dropdown #ddlCountry doesn't have custom attribute. You should target the <option> instead:

var ShipTo = $("#ddlCountry option:selected").attr("shipto");

Comments

2

Try. Getting value of selected option

$('#ddlCountry option:selected').attr('shipto');

Wroking DEMO

Comments

1

you should use data attributes instead

<select id="ddlCountry" runat="server" style="width: 300px; font-size: 9pt; float:left; margin-top: 3px;">
    <option data-shipto=''  value="">Select your location</option>
    <option data-shipto='GBR'  value="gb_en_home">United Kingdom - English</option>
    <option data-shipto='USA'  value="us_en_home">United States - English</option>
</select> 

and you access it like

var ShipTo = $("#ddlCountry option:selected").data("shipto");
alert(ShipTo );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.