Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery selector to get all select dropdowns with a particular value selected

Tags:

How do I select all the select boxes using a jQuery selector where value selected is "A1"?

Sample select box:

<select name="accesslevel"> <option value="A1">A1 Access</option> <option value="A2">A2 Access</option> </select> 

I have to use the selector to do the following thing:

.parent().nextAll('td[id^="A1Access"]').show(); .parent().nextAll('td[id^="A2Access"]').hide(); 

Can anyone help?

like image 667
ssahu Avatar asked Mar 01 '11 06:03

ssahu


People also ask

How can I get the selected value of a drop down list with jQuery?

Use the jQuery: selected selector in combination with val () method to find the selected option value in a drop-down list.

How can I get multiple selected values of select box in jQuery?

With jQuery, you can use the . val() method to get an array of the selected values on a multi-select dropdown list.

How do I get a dropdown list and selected value?

Method 1: Using the value property: The value of the selected element can be found by using the value property on the selected element that defines the list. This property returns a string representing the value attribute of the <option> element in the list. If no option is selected then nothing will be returned.

How do I get all dropdown options?

We can extract all the options in a dropdown in Selenium with the help of Select class which has the getOptions() method. This retrieves all the options on a Select tag and returns a list of web elements. This method does not accept any arguments.


2 Answers

As mentioned before, Prakash's solution is the closest, but it selects the option value rather than the select box itself. This can be fixed with the :has selector.

$('select:has(option[value="A1"]:selected)') 
like image 54
dsnettleton Avatar answered Nov 02 '22 07:11

dsnettleton


Try :

$('select option[value="A1"]:selected') 
like image 33
Prakash Avatar answered Nov 02 '22 09:11

Prakash