jQuery API

.context

context Returns: Element

Description: The DOM node context originally passed to jQuery(); if none was passed then context will likely be the document.

  • version added: 1.3context

The .live() method for binding event handlers uses this property to determine the root element to use for its event delegation needs.

The value of this property is typically equal to document, as this is the default context for jQuery objects if none is supplied. The context may differ if, for example, the object was created by searching within an <iframe> or XML document.

Note that the context property may only apply to the elements originally selected by jQuery(), as it is possible for the user to add elements to the collection via methods such as .add() and these may have a different context.

Example:

Determine the exact context 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="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  Context:<ul></ul>
<script>$("ul")
  .append("<li>" + $("ul").context + "</li>")
  .append("<li>" + $("ul", document.body).context.nodeName + "</li>");

</script>

</body>
</html>

Demo:

Support and Contributions

Need help with .context 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 .context? Report it to the jQuery core team.

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

* All fields are required
  • 534535

    35345

  • Anton

    it is not clear for me, where we are able to use this technologies?
    In any case we can use “parentNode” or simple “parent”. if sometime we need in some contex there are will be better create seprate object, that must be global and through them we can pass any data.

  • Anton

    it is not clear for me, where we are able to use this technologies?
    In any case we can use “parentNode” or simple “parent”. if sometime we need in some contex there are will be better create seprate object, that must be global and through them we can pass any data.

  • porizm

    Hey all

    I am using two versions of jQuery together (1.3.2 & 1.4.2)

    First, I embed the old version, then I use this method for conflict:
    var jQuery1=jQuery.noConflict(true)

    then, I embed the newer version

    It's working well, but context method is not working, why??