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? Commented Dec 19, 2012 at 9:35
  • 1
    @zerkms he is after textbox though not checkbox, unless I am reading this wrong. Commented Dec 19, 2012 at 9:36
  • 1
    @Zerkms, he is asking for selected text inside a textbox. (I guess) Commented Dec 19, 2012 at 9:36
  • 1
    Hm... Why does the background's color matter anyway? :-" 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. 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... ​

Sign up to request clarification or add additional context in comments.

3 Comments

this code returns blank. Does not return any value.
Cool the second function returns something :) Thanks @A.V.
Happy to be Helpful...:)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.