codeBoy
codeBoy

Reputation: 533

Grabbing Dropdown Selection Parent JavaScript

I am trying to store the parent of a dropdown selection into a variable. I am able to do it using this line of code:

var myVariable = document.getElementById('mydropdownlist');

But I want to pull it using a selection in the dropdown list: So something like this, but it is not working

var mySecondVariable = $('select option:contains("New York")').parent();

Can anyone tell me what the issue is with the second line of code?

Upvotes: 2

Views: 40

Answers (1)

FabioG
FabioG

Reputation: 2986

the code is storing the select as an object that contains the select itself instead of storing it as an object of type HTMLSelectElement as you want, if you use

var mySecondVariable = $('select option:contains("New York")').parent()[0];

or

var mySecondVariable = $('select option:contains("New York")').parent().get(0);

it should work perfectly, although I'm not sure if there's any better solution, if so someone should state it.

in jQuery FAQs

Upvotes: 1

Related Questions