Pages Menu
TwitterRssFacebook
Categories Menu

Posted by in jQuery Resources & Tutorials | 2 Comments

jQuery Tip: How to find out if the text in a textbox is selected with jQuery

Advertisement

Here, we’re sharing a jQuery tip. This tip will help you to know if the text in a text box isĀ <input type=”text” /> selected or not. The result will be true or false.

 

You might find these useful

HTML Code

<input id="text_selected" type="text" value="Select Text" />
<input onmousedown="alert(isTextSelected(document.getElementByID('text_selected')));" type="button" value="Selected or Not" />

In HTML code, there is one textbox with id “text_selected“. We are triggering a function on a button and alerting the results.

jQuery Code

function isTextSelected(input) {
    if (typeof input.selectionStart == "number") {
        return input.selectionStart == 0 &amp;&amp; input.selectionEnd == input.value.length;
    } else if (typeof document.selection != "undefined") {
        input.focus();
        return document.selection.createRange().text == input.value;
    }
}

Above jQuery code will tell you whether or not all of the text is selected within a text input. It will work on all major browsers.

Post a Reply

Your email address will not be published. Required fields are marked *


five × 1 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>