How to select first option using jquery

How to select first option using jquery

Posted: CashToChange Date: 21.07.2017

Tutorial by Matt Doyle Level: Beginner Published on 8 March Learn how to use jQuery selectors in this tutorial.

Includes a description of all selectors supported by jQuery, with example code for each selector. As explained in Getting Started with jQuerytypically you use jQuery selectors to locate elements within the page to work on, then call jQuery methods to manipulate those elements.

In this tutorial you'll look at pretty much all of the selectors supported by jQuery.

Ajax Tutorial: Dynamic Loading of ComboBox using jQuery and Ajax in PHP | 99Points

Many of these are the same as CSS selectors, while some are specific to jQuery. For each selector you'll see an example showing how to use the selector to find page elements. Here's a list of all selectors supported by jQuery.

The basic selectors will be familiar to you if you've ever built a CSS layout or two. You can also combine multiple selectors into a single selector using commas. The basic selectors are great if, for example, you want to select all paragraphs in the page, or the element you want to select has a CSS class or ID.

However, sometimes you need to select a specific element that doesn't have a class or ID, and there's no easy way to add a class or ID to the element in the markup. This can happen when using a CMS with a fixed HTML template, or when working with user-created content. In this situation, you may be able to narrow down the selection by looking at the HTML attributes of the element that you want to select.

For example, you could select:. If you can't narrow down your selection with the basic or attribute selectors, you can delve into an element's content to determine if it should be selected or not. The following examples show how to use: Another way to select elements in jQuery is to look at how elements relate to each other in the page.

You're probably familiar with many of these selectors from working with CSS:. As well as specifying numbers with: Find out more about: The following example selects the first cell of all the odd rows in a table that has an id of "myTable":. Form elements have some specific properties that can make them slightly trickier to select. Similarly, identifying the selected items in a select list, or the checked checkboxes in a form, can be tedious. This is true for jQuery in general — to keep your code fast, always restrict the scope of your selectors where possible.

The following example looks for the form that has an action attribute of "mailform.

jquery - Setting first