Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check the value in textbox is selected using jquery [duplicate]

Tags:

Possible Duplicate:
Check checkbox checked property using jQuery

Can anyone let me know how to check the value is selected in the html textbox using jquery. For example the value in the textbox if selected the text is slected with blue background.

like image 725
teenu Avatar asked Dec 19 '12 09:12

teenu


People also ask

How can check duplicate value in textbox using jquery?

Using each() check value of inputs and if any value is duplicate add class duplicate to it.

How to check duplicate values in table jquery?

var contents = {}, duplicates = false; $("#yourtableidhere td"). each(function() { var tdContent = $(this). text(); if (contents[tdContent]) { duplicates = true; return false; } contents[tdContent] = true; }); if (duplicates) alert("There were duplicates.");


1 Answers

See this : http://jsfiddle.net/rT5vR/

var t = '';
if(window.getSelection) {
    t = window.getSelection();
} else if(document.getSelection) {
    t = document.getSelection();
} else if(document.selection) {
    t = document.selection.createRange().text;
}
return t;
}

$("#myElement").select(function(eventObject) {
alert(getSelected().toString());
});

​Or

$('#myElement').select(function(e) {
var start = e.target.selectionStart;
var end = e.target.selectionEnd;
alert($('#myElement').val().substring(start, end));
});

The second one is perfect. Don't know how much cross-browser the first one is... ​

like image 149
Anujith Avatar answered Oct 09 '22 18:10

Anujith