2

I have inherited an MVC forms that emit selects dynamically identified and named by a prefix, a guid, and a suffix. For example,

<select id="N9_ID_f0d7ca03-0221-41b7-a457-3494b9e483e3__N9QualifierID" name="N9[ID_f0d7ca03-0221-41b7-a457-3494b9e483e3].N9QualifierID"> <option value="58">Automated Export System (AES) Internal Transaction Number (ITN)</option> <option value="60">Automated Export System Export Information Code Exemption Number (Note 1)</option> <option value="59">Automated Export System Option 4 Employee Identification Number</option> <option value="8">Bill of Lading</option> <select> 

There will be several selects with the same prefix and suffix. The prefix and suffix effectively partitions the selects into a class and guid ties the dynamically generated selects to other dynamically generated controls such as textboxes. I need to find the the select that has a given value so I may extract the guid and do stuff with other inputs that have that guid.

My Question:

Using JQuery, how do I select the select(s) that have a given selected value? For example, I know at least one of the selects will have a selected value of 59, but I don't know which one.

2 Answers 2

2

This will return all selects with an option that has 59 as its value.

var selects = $('option[value="59"]').parent(); 

If you want the ones that have the 59 as their currently selected value then

var selects = $('option[value="59"]:selected').parent(); 

Then, once you have the selects, you can use the .each() to do what you want with them

selects.each(function(){ // extract guid and do whatever.. }); 
Sign up to request clarification or add additional context in comments.

Comments

0

You probably need the property, not the attribute (which doesn't change), so using a filter should work

$('select').filter(function() { return this.value === '59'; }).prop('id'); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.