I have a select box defined as shown below. I want to print out the name and email address of each item in the select box as ma seperated values, like
Tom Wayne,[email protected]
Joe Parker,[email protected]
Peter Simons,[email protected]
Any way to acplish that using JQuery?
<select multiple="multiple" name="search_results">
<option value="[email protected]">Tom Wane</option>
<option value="[email protected]">Joe Parker</option>
<option value="[email protected]">Peter Simons</option>
</select>
Thank You
I have a select box defined as shown below. I want to print out the name and email address of each item in the select box as ma seperated values, like
Tom Wayne,[email protected]
Joe Parker,[email protected]
Peter Simons,[email protected]
Any way to acplish that using JQuery?
<select multiple="multiple" name="search_results">
<option value="[email protected]">Tom Wane</option>
<option value="[email protected]">Joe Parker</option>
<option value="[email protected]">Peter Simons</option>
</select>
Thank You
Share Improve this question edited Jan 5, 2010 at 15:03 Ken White 126k15 gold badges236 silver badges464 bronze badges asked Jan 5, 2010 at 14:59 ScottScott 134 bronze badges3 Answers
Reset to default 10I think is a good example to use Traversing/map:
$('select option').map(function () {
return $(this).text() + ',' + $(this).val();
}).get().join('\n');
Try this:
$("select").each(function() {
var options = [];
$(this).children("option").each(function() {
var $this = $(this);
options.push($this.text()+ "," + $this.val());
});
alert(options.join("\n"));
});
This will alert you the options for each select individually.
Something like this:
var s = "";
$("select option").each(function()
{
var $option = $(this);
s += $option.text() + ", " + $option.val() + "\n";
});
alert(s);