tessa
tessa

Reputation: 838

selecting a select list in jquery

There has to be something really simple that I am overlooking, but for some reason I cannot find any information about this and it keeps coming up so..

I am trying to select a select element in jquery. Right now I don't care about it's options, I just want the select element. If I have the following element:

<select id="testSelect"><option>1</option></select>

and I use jquery to select it:

var selectElement = $("#testSelect");

I get an array of all the options in the select list, but not the select element itself. What am I missing?

Upvotes: 1

Views: 264

Answers (1)

Scott McMillin
Scott McMillin

Reputation: 531

You are selecting it correctly.

var selectElement = $("#testSelect");

Remember though that selectElement is now a jQuery object not a DOM node. This allows you to use jQuery methods to manipulate it as follows:

selectElement.css({'background-color':'red', 'border':'1px solid'});

selectElement.hide();

selectElement.show();

If you wish to access the DOM node directly use the get method as outlined here: http://api.jquery.com/get/. So for example if you want to access the DOM nodes style property you would do it like this:

$('#tl_query_season').get(0).style

and get background-color like this:

$('#tl_query_season').get(0).style.backgroundColor

But if you're doing this you're missing out on all the great jQuery functionality.

See Also: http://api.jquery.com/css/ and http://api.jquery.com/category/effects/

Upvotes: 1

Related Questions