jQuery selected selector

Suggested Videos
Part 11 - jQuery checked selector
Part 12 - Select values of checkbox group with jquery
Part 13 - jQuery get selected checkbox text

(追記) (追記ここまで)

In this video we will discuss jQuery :selected selector.

(追記) (追記ここまで)

To select all checked checkboxes or radio buttons, we use :checked selector. To select all selected options of a select element use :selected selector.

How to get selected option from single select dropdown in jquery : We want to get the selected option text and value
jquery get selected option value

<html>
<head>
<title></title>
<scriptsrc="jquery-1.11.2.js"></script>
<scripttype="text/javascript">
$(document).ready(function () {
$('#selectCountries').change(function () {
var selectedOption = $('#selectCountries option:selected');
$('#divResult').html('Value = ' + selectedOption.val()
+ ', Text = ' + selectedOption.text());
});
});
</script>
</head>
<bodystyle="font-family:Arial">
Country:
<selectid="selectCountries">
<optionselected="selected"value="USA">United States</option>
<optionvalue="IND">India</option>
<optionvalue="UK">United Kingdom</option>
<optionvalue="CA">Canada</option>
<optionvalue="AU">Australia</option>
</select>
<br/><br/>
<divid="divResult"></div>
</body>
</html>

How to get all selected options from multi-select dropdown in jquery : We want to get all the selected options text and value.
jquery get multiple selected option value

<html>
<head>
<title></title>
<scriptsrc="jquery-1.11.2.js"></script>
<scripttype="text/javascript">
$(document).ready(function () {
$('#selectCountries').change(function () {
var selectedOptions = $('#selectCountries option:selected');
if (selectedOptions.length > 0) {
var resultString = '';
selectedOptions.each(function () {
resultString += 'Value = ' + $(this).val() +
', Text = ' + $(this).text() + '<br/>';
});
$('#divResult').html(resultString);
}
});
});
</script>
</head>
<bodystyle="font-family:Arial">
<selectid="selectCountries"multiple="multiple">
<optionselected="selected"value="USA">United States</option>
<optionvalue="IND">India</option>
<optionvalue="UK">United Kingdom</option>
<optionvalue="CA">Canada</option>
<optionvalue="AU">Australia</option>
</select>
<br/><br/>
<divid="divResult"></div>
</body>
</html>

Please note : Hold down the CTRL key, to select more than one item.

jQuery tutorial for beginners

1 comment:

  1. Many many thanks, for your helpful all tutorial . how are you, sir ? Sir multiple selectors do not work.When I clicked any option then change and Replaced text.

    Reply Delete

It would be great if you can help share these free resources

[フレーム]

Subscribe to: Post Comments (Atom)

AltStyle によって変換されたページ (->オリジナル) /