.selector

selector Returns: String

Description: A selector representing selector originally passed to jQuery().

  • version added: 1.3selector

Should be used in conjunction with context to determine the exact query used.

The .live() method for binding event handlers uses this property to determine how to perform its searches. Plug-ins which perform similar tasks may also find the property useful. This property contains a string representing the matched set of elements, but if DOM traversal methods have been called on the object, the string may not be a valid jQuery selector expression. For this reason, the value of .selector is generally most useful immediately following the original creation of the object. Consequently, the .live() method should only be used in this scenario.

Examples:

Example: Determine the selector used.

<!DOCTYPE html>
<html>
<head>
  <style>
  body { cursor:pointer; }
  div { width:50px; height:30px; margin:5px; float:left;
        background:green; }
  span { color:red; }
  </style>
  <script src="https://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
	Some selectors:<ul></ul>
<script>$("ul")
  .append("<li>" + $("ul").selector + "</li>")
  .append("<li>" + $("ul li").selector + "</li>")
  .append("<li>" + $("div#foo ul:not([class])").selector + "</li>");

</script>
</body>
</html>

Demo:

Example: Collecting elements differently

<!DOCTYPE html>
<html>
<head>
  <script src="https://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
	Some selectors:<ul></ul>

<script>
   $('<div>' + $('ul li.foo').selector + '</div>').appendTo('body');  // "ul li.foo"
   $('<div>' + $('ul').find('li').filter('.foo').selector + '</div>').appendTo('body'); // "ul li.filter(.foo)"
</script>
</body>
</html>

Demo: