tags:

views:

43

answers:

2

I have select values as follows:

<select id="SelectBox" multiple="multiple">

            <% foreach (var item in Model.Name)
            { %>
                <option value="<%= item.Value %>"><%=item.Text%></option>
            <% } 
            %>

            </select>

I have a function in jquery that will have to read both the text and value. I need to have the values in array so that I can display them in a table with a column Id and another column text. My problem is that Im not able to retrieve each and every value separately. Im having the text in one line, test1test2test3.

function read() {
            $("#SelectBox").each(function() {
                var value = $(this).val();
                var text = $(this).text();
                alert(value);alert(text);

            });
        }
A: 

You're close. You need to iterate over the <option>s, not the <select> elements:

$("#SelectBox option").each(function() {
  var value = $(this).val();
  var text = $(this).text();
  alert(value);
  alert(text);
}
cletus
+1  A: 

Try

function read() {
            $("#SelectBox > option").each(function() {
                var value = $(this).val();
                var text = $(this).text();
                alert(value);alert(text);

            });
        }
rahul