jQuery API

Attribute Contains Selector [name*="value"]

attributeContains selector

version added: 1.0jQuery('[attribute*="value"]')

  • attribute
    An attribute name.
    value
    An attribute value. Can be either an unquoted single word or a quoted string.

Description: Selects elements that have the specified attribute with a value containing the a given substring.

This is the most generous of the jQuery attribute selectors that match against a value. It will select an element if the selector's string appears anywhere within the element's attribute value. Compare this selector with the Attribute Contains Word selector (e.g. [attr~="word"]), which is more appropriate in many cases.

Example:

Finds all inputs with a name attribute that contains 'man' and sets the value with some text.

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <input name="man-news" />

  <input name="milkman" />
  <input name="letterman2" />
  <input name="newmilk" />
<script>$('input[name*="man"]').val('has man in it!');</script>

</body>
</html>

Demo:

Support and Contributions

Need help with Attribute Contains Selector [name*="value"] 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 Attribute Contains Selector [name*="value"]? Report it to the jQuery core team.

Found a problem with this documentation? Report it to the jQuery API team.

* All fields are required
  • http://siderite.blogspot.com/ Siderite

    What about “not containing” ? Or bigger and less than (for tabIndex, for example)

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

      For “not containing,” you can do this:$(‘:not([attr*=value])’)or this:$(something).not([attr*=value]‘)Of course, these are “pseudo-code” examples. You need to put in your own selectors, attributes, and values.

      • Rodrigo T.

        Very cool function.Thanks!

  • teddyber

    what about a case-insensitive search? is that doable?

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

      You can use .filter(fn) for case-insensitive attribute matches. For example, if you wanted to select only those elements with an href that matches “some value” case-insensitively, you could do this:

      $('your-selector').filter(function() {
      return /some value/i.test(this.href);
      });

  • BananaAcid

    case insensitive wih user input

    window.keyword = “abc”;
    $(selector).filter(function(keyword) {return $(this).attr(“attribute”).toLowerCase().indexOf(keyword.toLowerCase()) != -1; });

  • Anonymous

    Is that possible to select with many attribute – value pairs?. For example something like:
    $(‘input[type=checkbox, checked=true]‘)

    • Antonio

      You can use it like this,
      $(‘input[type=checkbox][checked=true]‘)

      • Karolowaty

        What kind of connection is this [type=checkbox][checked=true] ?? Alternative ?? Conjuction ??

        • samllrabbit

          u can also use it like this:
          $(“input[type='checkbox']:checked”)

  • Abc

    Just wanna make sure we can use $(“select[name*='abc']“)…..i just cannot get it selected.thanks

  • http://zerointeger.tumblr.com Jason Gerfen

    I am experiencing a problem that I have not been able to resolve with research.

    Using a group of nested form elements (IE. <input id=”txt[0][amount]“>), I am not able to come up with a method of selecting and assigning a bind event to anything matching the specified group (IE $j('input[name*=txt\[*\]\[amount\]]').bind('blur', function() { // operations });)

    Is this possible? Any help is definitely appreciated.

  • http://zerointeger.tumblr.com Jason Gerfen

    I am experiencing a problem that I have not been able to resolve with research.

    Using a group of nested form elements (IE. <input id=”txt[0][amount]“>), I am not able to come up with a method of selecting and assigning a bind event to anything matching the specified group (IE $j('input[name*=txt\[*\]\[amount\]]').bind('blur', function() { // operations });)

    Is this possible? Any help is definitely appreciated.

  • Jimmyt1988

    Is there a way of selecting elements where two pseudos = something:

    $(tr[year="this" & section = "that"])

  • Jimmyt1988

    Is there a way of selecting elements where two pseudos = something:

    $(tr[year="this" & section = "that"])

  • Jimmyt1988

    Is there a way of selecting elements where two pseudos = something:

    $(tr[year="this" & section = "that"])

  • Jimmyt1988

    Is there a way of selecting elements where two pseudos = something:

    $(tr[year="this" & section = "that"])

  • Jimmyt1988

    Is there a way of selecting elements where two pseudos = something:

    $(tr[year="this" & section = "that"])

  • Jimmyt1988

    Is there a way of selecting elements where two pseudos = something:

    $(tr[year="this" & section = "that"])

  • Jimmyt1988

    Is there a way of selecting elements where two pseudos = something:

    $(tr[year="this" & section = "that"])

  • Jimmyt1988

    Is there a way of selecting elements where two pseudos = something:

    $(tr[year="this" & section = "that"])

  • Jimmyt1988

    Is there a way of selecting elements where two pseudos = something:

    $(tr[year="this" & section = "that"])

  • Jimmyt1988

    Is there a way of selecting elements where two pseudos = something:

    $(tr[year="this" & section = "that"])

  • Jimmyt1988

    Is there a way of selecting elements where two pseudos = something:

    $(tr[year="this" & section = "that"])

  • Jimmyt1988

    Is there a way of selecting elements where two pseudos = something:

    $(tr[year="this" & section = "that"])

  • Jimmyt1988

    Is there a way of selecting elements where two pseudos = something:

    $(tr[year="this" & section = "that"])

  • Jimmyt1988

    Is there a way of selecting elements where two pseudos = something:

    $(tr[year="this" & section = "that"])

  • Jimmyt1988

    Is there a way of selecting elements where two pseudos = something:

    $(tr[year="this" & section = "that"])

  • Jimmyt1988

    Is there a way of selecting elements where two pseudos = something:

    $(tr[year="this" & section = "that"])

  • Jimmyt1988

    Is there a way of selecting elements where two pseudos = something:

    $(tr[year="this" & section = "that"])

  • samllrabbit

    u can also use it like this:
    $(“input[type='checkbox']:checked”)

  • Houston

    can i do this?
    var cell = “a”;
    $('input[name=cell']);

    if not what is the alternative? or it is impossible to pass value from a variable?

    • vx

      you can do this

      $('input[name="'+cell+'"]');

  • Vicky

    Can anybody tell me the link of jquery validation..?

  • http://twitter.com/freejazznet Free Jazz Network

    Can anybody tell me why this code doesn't work? It may be getting too late for me but I really do not see why it is failing…

    $('a[class*="fancybox"]').filter(':first').trigger('click');

  • http://twitter.com/freejazznet Free Jazz Network

    Ah, just found out the code is good. There was something else going wrong. Sorry, it IS getting late.

    Moderator: please remove this thread, thanks :)

  • Akegalj

    is this working on ie6? Because i cant figure out why not.

  • teddyber

    found this workaround:
    $('.myClass').filter(function() {return (new RegExp($('#q2').attr('valueToTest').toLowerCase(), “i”)).test($(this).attr('attrToTest'));});

    I also needed to be accent-insensitive, I just added a “stripAccent” custom function to deal with that…