function Myfunc(obj1)
{
alert(obj1.prop('outerHTML'));
}
<select id="myselect" onchange="MyFunc(jQuery(this))">
<option value="v1">Value 1</option>
<option value="v2">Value 2</option>
<option value="v3" selected="selected">Value 3</option>
</select>
I have the above two snippets. When i do alert of the object i get the complete <select&tg; tag along with all it's options.
But, What i want is the object passed should only just be the option which i have selected and not the complete select element block.
-
yes i wanted the complete HTML of the option selected, and i am really thankful to @Regent for the answer.Prashant– Prashant2014年11月06日 08:44:17 +00:00Commented Nov 6, 2014 at 8:44
5 Answers 5
Instead of outdated onchange you can use jQuery $('selector').change(function() { });
Simplified HTML:
<select id="myselect">
<option value="v1">Value 1</option>
<option value="v2">Value 2</option>
<option value="v3" selected="selected">Value 3</option>
</select>
JS:
$(document).ready(function()
{
$('#myselect').change(function()
{
alert($(this).find(':selected').text());
});
});
Update. If you need selected <option> outer HTML, then it can be:
$(document).ready(function()
{
$('#myselect').change(function()
{
alert($(this).find(':selected')[0].outerHTML);
});
});
3 Comments
<option value="v1">Value 1</option> is requested, not only its text value? I actually read question.Try:
onchange="MyFunc(jQuery(this).find('option:selected'))"
5 Comments
Use as
function MyFunc(obj1)
{
alert($("#myselect option:selected").text());
}
OR You can use
function MyFunc(obj1)
{
alert($("#myselect option:selected").html());
}
Comments
You can pass object or you can fetch selected option in function.
To get selected option html in function use below code -
NOTE - please correct spelling of function either in onchange or function declaration.
function MyFunc(obj1)
{
var value = obj1.value;
var optionHTML = $(obj1).find('option[value="'+value+'"]');
alert($('<div>').append(optionHTML.clone()).html());
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<select id="myselect" onchange="MyFunc(this)">
<option value="v1">Value 1</option>
<option value="v2">Value 2</option>
<option value="v3" selected="selected">Value 3</option>
</select>
Another way is to bind change event handler using jquery and get the option html
$('#myselect').change(function(){
var value = $(this).val();
var option = $(this).find('option[value="'+value+'"]');
var optionHTML = $('<div>').append(option.clone()).html();
alert(optionHTML);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<select id="myselect">
<option value="v1">Value 1</option>
<option value="v2">Value 2</option>
<option value="v3" selected="selected">Value 3</option>
</select>
Comments
You can use :selected selector follows:
$(document).ready(function() {
$('#myselect').on('change', function() {
alert($(this).find('option:selected').eq(0));
});
});
5 Comments
.eq(0)?