mirza
mirza

Reputation: 5793

How to get a value from second element of a class with jquery?

my html code:

        <select class="b_selectbox">
            <option value="0">Status</option>
        </select>

        <select class="b_selectbox">
            <option value="0">Type</option>
        </select>

        <select class="b_selectbox">
            <option value="0">Category</option>
        </select>

That's working for first element:

$(".b_selectbox option:first").text();

I am trying to get text "Type", here is what i tried so far:

 $(".b_selectbox option:first").text()[1]; // result: "t" probably second letter from "Status"

$(".b_selectbox option:first")[1].text(); // not working either

Is there a solution without using each and id names ?

Upvotes: 8

Views: 13693

Answers (3)

MyStream
MyStream

Reputation: 2543

$(".b_selectbox option:first").is(":selected").text() ?

Upvotes: 0

Felix Kling
Felix Kling

Reputation: 816334

Either

$('.b_selectbox option').eq(1).text();

if each select element has only one option (makes a select unnecessary?), or if you want to get the second of all options, or

$('.b_selectbox').eq(1).children('option').first().text();

if you want the text of the first option of the second select element.

For more information, see .eq() [docs].

Upvotes: 20

Selvakumar Arumugam
Selvakumar Arumugam

Reputation: 79830

I think you are looking for :eq operator... Try,

$(".b_selectbox:eq(1) option:first").text()

DEMO

Upvotes: 3

Related Questions