How do I put text in a drop down list?

We can select text or we can also find the position of a text in a drop down list using option:selected attribute or by using val[] method in jQuery.

By using val[] method :
The val[] method is an inbuilt method in jQuery which is used to return or set the value of attributes for the selected elements.

Syntax :

$[selector].val[parameter]

Parameter : parameter is optional.

Example :






jQuery Selector
$[document].ready[function[] {
$["#submit"].click[function[] {
alert[$["#myselection"].val[]];
}];
}];
div {
width: 50%;
height: 200px;
padding: 10px;
border: 2px solid green;
}

The selected value:

First
Second


Submit

Output :
Before click on the button.


After click on the button.

By using option:selected :
The option:selected method is a way in jQuery which is used to return the selected element from a list of the element.

Syntax :

$["#selector option:selected"];

Parameter : No parameter required.

Example :




jQuery Selector
$[document].ready[function[] {
$["#submit"].click[function[] {
var value = $["#myselection option:selected"];
alert[value.text[]];
}];
}];
div {
width: 50%;
height: 200px;
padding: 10px;
border: 2px solid green;
}

The selected value:

First
Second


Submit

Output :
Before click on the button.


After click on the button.

jQuery is an open source JavaScript library that simplifies the interactions between an HTML/CSS document, It is widely famous with its philosophy of Write less, do more.
You can learn jQuery from the ground up by following this jQuery Tutorial and jQuery Examples.




Article Tags :
JQuery
Web Technologies
jQuery-Misc

Video liên quan

Chủ Đề