jQuery API

:text Selector

text selector

version added: 1.0jQuery(':text')

Description: Selects all elements of type text.

$(':text') allows us to select all <input type="text"> elements. As with other pseudo-class selectors (those that begin with a ":") it is recommended to precede it with a tag name or some other selector; otherwise, the universal selector ("*") is implied. In other words, the bare $(':text') is equivalent to $('*:text'), so $('input:text') should be used instead.

Note: As of jQuery 1.5.2, :text selects input elements that have no specified type attribute (in which case type="text" is implied).

This difference in behavior between $(':text') and $('[type=text]'), can be seen below:

$('<input>').is('[type=text]'); // false
$('<input>').is(':text'); // true

Additional Notes:

  • Because :text is a jQuery extension and not part of the CSS specification, queries using :text cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. For better performance in modern browsers, use [type="text"] instead.

Example:

Finds all text inputs.

<!DOCTYPE html>
<html>
<head>
  <style>
  textarea { height:25px; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <form>
    <input type="button" value="Input Button"/>
    <input type="checkbox" />

    <input type="file" />
    <input type="hidden" />
    <input type="image" />

    <input type="password" />
    <input type="radio" />
    <input type="reset" />

    <input type="submit" />
    <input type="text" />
    <select><option>Option</option></select>

    <textarea></textarea>
    <button>Button</button>
  </form>
  <div>
  </div>
<script>

    var input = $("form input:text").css({background:"yellow", border:"3px red solid"});
    $("div").text("For this type jQuery found " + input.length + ".")
            .css("color", "red");
    $("form").submit(function () { return false; }); // so it won't submit

</script>

</body>
</html>

Demo:

Support and Contributions

Need help with :text Selector or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to :text Selector? Report it to the jQuery core team.

Found a problem with this documentation? Report it on the GitHub issue tracker

  • Kevin Peno

    Note that this selector does not include textarea with it.

    • http://www.jQueryEklenti.com/ Sedat Kumcu

      When you like select a textarea use below selector,
      $(“textarea”).show();
      or
      $(“textarea.SomeClass”).show(); //texarea classname is SomeClass

      • Kevin Peno

        One should really overload the :text selector and fix this, but I found a reasonable work around for now. What's great is that $(“textarea”).val() returns the same value as $(“textarea”).text(). This means it can be treated just like $(“:text”).val(). So use this when you want to find all text-like boxes and check their values.

        $(“:text,textarea”).val();

  • Chris J

    There is a Typo on the <option> tag in the example.
    <select><option>Option<option/></select>
    At least running it within an Apex environment it should read
    <select><option>Option</option></select>

  • http://www.facebook.com/people/Ron-Lussier/505036625 Ron Lussier

    It would be GREAT to have a selector that selected all text-input fields. Right now I think I need to so something like:
    input[type=text],input[type=email],input[type=url],input[type=number],input[type=search], etc.

    • http://www.learningjquery.com/ Karl Swedberg

      Maybe you could do something like this?
      $('input').not(':password,:checkbox,:radio');

      • Liucheng

        just like:$(“:text”)

  • Liucheng

    just like:$(“:text”)