Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get highlighted text using jquery .select()?

Tags:

jquery

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

like image 675
Serjas Avatar asked Aug 31 '12 08:08

Serjas


People also ask

How do I get selected text option in select?

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.

How do I get the selected value and current selected text of a dropdown box using jQuery?

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.


1 Answers

.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/

like image 99
glomad Avatar answered Oct 03 '22 21:10

glomad