Kumar
Kumar

Reputation: 741

How to get name of the dropdown selected item in jquery?

If i have the bookd_ids and book_titles as follows in the database:

 id   |  title
 ---------------
 1    |  abc
 2    |  xyz
 3    |  pqr

I have the books dropdownlist as show below:

<%=  select("books", "book_id", Book.all.collect {|b|
                [ b.title, b.id ] }, {:include_blank => 'Select Book'})%>

i can get the id of the books thru query this way:

var id = $("#books_book_id").val();

How to get the abc, xyz pqr titles using jquery?

Upvotes: 5

Views: 21613

Answers (2)

Mike Nguyen
Mike Nguyen

Reputation: 1053

Have put some space between :selected

var value = jQuery("#edit-system-name :selected").text();

Upvotes: 3

Sagi
Sagi

Reputation: 8011

var text = $("#books_book_id option:selected").text();

Upvotes: 13

Related Questions