Kumar
Kumar

Reputation: 741

How to get values of multiple selected ids

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

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

The view is shown below::

<table>
<tr>
  <td>
     <%=  select("books", "book_id", Book.all.collect {|b|
           [ b.title, b.id ] },{:include_blank => false})%>
  </td>
  <td>
     <img src="/images/add_icon.png"
        alt="Add Book" width="20px" height="20px" onclick="AddBook()" />
  </td>
</tr>
<tr>
   <td id="current">
     <br />
     Current Books<br />
     <p id="none" style="font-style: italic; font-weight: lighter">
     None Selected</p>
   </td>
</tr>
<tr>
   <td id="tdbook"></td>
   <td id="tdbookimg"></td>
</tr>
</table>

When i click on the add_icon.png image, it adds the selected book under selectedbooks header. If i do it multiple times it add multiple selected rows under selected books.

the AddBook() function is written as:

function AddBook(){
count = count + 1;
$('#none').hide();

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

$("#tdbook").append("<p id='row" + id + "'><label for='txt" + id + "'>" +$("#books_book_id option:selected").text() + "</label></p>");
$("#tdbookimg").append("<p id='rowimg" + id + "'></p>");
}

i can get the id of one book thru jquery this way:

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

How to get multiple ids when multiple items are added. So that i can get the values in the controller.

Thanks, Kumar

Upvotes: 1

Views: 873

Answers (1)

davecoulter
davecoulter

Reputation: 1826

Kumar-- try giving selected books a class like:

$("#books_book_id").addClass("selected");

So that later you can select them all by:

$(".selected").val();

Upvotes: 1

Related Questions