Ricardo Binns
Ricardo Binns

Reputation: 3246

get each value from select multiple

easy one guys:

i have a select, type multiple, so i need to do a loop to send to database each value that was selected, one by one.

this is what i have done so far, but my $.each are returning 0 1 2 3.

how can i do this ?

html:

   <select multiple="multiple" id="sel">
          <option>All</option>
          <option>Option 1</option>
          <option>Option 2</option>
          <option>Option 3</option>
  </select>

js:

    $("#send").click(function(){
         var sel = $("#sel").val();
         $.each(sel, function(e){
             alert(e); // should come the text of each one
         });
    });

Thanks.

Upvotes: 0

Views: 4280

Answers (5)

jbabey
jbabey

Reputation: 46657

http://jsfiddle.net/wfqa7/3/

you need the value in the $.each, not the index. jQuery.each gives these two parameters to your callback function.

Upvotes: 0

kojiro
kojiro

Reputation: 77167

Assuming you mean a server-side database, the next step is to set up a server side application that can insert the rows into the database that you're sending it from jQuery.

On the jQuery side you probably want something like: $.post(someurl, sel);

This is based on the assumptions made in your question, but I'm not actually sure what you expect sel to contain here. You may want to look at jQuery.serialize or serializeArray.

Upvotes: 0

Sedat Başar
Sedat Başar

Reputation: 3798

You show the key not the value. try this

$.each(sel, function(key,value){
             alert(value); // should come the text of each one
         });

Upvotes: 1

Irishka
Irishka

Reputation: 1136

e = index , not an array element itself

$.each(sel, function(e){
    alert(sel[e]); // should come the text of each one
});

Upvotes: 0

lauhub
lauhub

Reputation: 920

If I undestand well, you want to retrieve the html content of each value.

So you should do this:

     $.each(sel, function(index, element){
         alert(element.html());
     });

Upvotes: 0

Related Questions