21

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.

5
  • Can you expand on this a bit? Its a bit unclear at the moment. What have you tried so far?
    – Gaz Winter
    Commented Dec 19, 2012 at 9:35
  • 1
    @zerkms he is after textbox though not checkbox, unless I am reading this wrong.
    – Gaz Winter
    Commented Dec 19, 2012 at 9:36
  • 1
    @Zerkms, he is asking for selected text inside a textbox. (I guess)
    – XCS
    Commented Dec 19, 2012 at 9:36
  • 1
    Hm... Why does the background's color matter anyway? :-"
    – XCS
    Commented Dec 19, 2012 at 9:37
  • @GazWinter : I mean the textbox. the color is just to make understand its not a select or a radio button. Got commond to select commond like window.selected() for outside the textbox but I am looking something which can get the selected value in the textbox. Please Help.
    – teenu
    Commented Dec 19, 2012 at 9:42

1 Answer 1

9

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

3
  • this code returns blank. Does not return any value.
    – teenu
    Commented Dec 19, 2012 at 9:47
  • Cool the second function returns something :) Thanks @A.V.
    – teenu
    Commented Dec 19, 2012 at 9:49
  • Happy to be Helpful...:)
    – Anujith
    Commented Dec 19, 2012 at 9:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.