I was trying example code found here http://api.jquery.com/select/
$(":input").select( function () {
$("div").text("Something was selected").show().fadeOut(1000);
});
Now my question is instead of printing "Something was selected" , can I get exact selected text?
I want jquery .select() specific answers . I got other solution from here
Use the selectedIndex Property We can get the index of the selected item with the selectedIndex property. Then we can use the text property to get the text of the selected item. We create the getSelectedText function that takes an element as the parameter. Then we check if selectedIndex is -1.
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.
.select()
has nothing to do with retrieving the actual selected text. Select is just like .click()
: an easy, shorthand way way to bind a handler function to the select event.
It says right in the API docs:
The method for retrieving the current selected text differs from one browser to another. A number of jQuery plug-ins offer cross-platform solutions.
So you bind with element.select(...)
(or better still, element.on("select", ...)
), and then you use one of the many cross-platform solutions to retrieve the selection. This is the basic idea: http://jsfiddle.net/NjW5a/3/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With