-
parent( ) returns Array<Element>
Matches all elements that are parents - they have child elements, including text.
Example:
Finds all tds with children, including text.
$("td:parent").fadeTo(1500, 0.3);HTML:
<table border="1"> <tr><td>Value 1</td><td></td></tr> <tr><td>Value 2</td><td></td></tr> </table> -
parent( String expr ) returns jQuery
Get a set of elements containing the unique parents of the matched set of elements.
You may use an optional expression to filter the set of parent elements that will match.Example:
Shows the parent of each element as (parent > child). Check the View Source to see the raw html.
$("*", document.body).each(function () { var parentTag = $(this).parent().get(0).tagName; $(this).prepend(document.createTextNode(parentTag + " > ")); });HTML:
<div>div, <span>span, </span> <b>b </b> </div> <p>p, <span>span, <em>em </em> </span> </p> <div>div, <strong>strong, <span>span, </span> <em>em, <b>b, </b> </em> </strong> <b>b </b> </div>Example:
Find the parent element of each paragraph with a class "selected".
$("p").parent(".selected").css("background", "yellow");HTML:
<div><p>Hello</p></div> <div class="selected"><p>Hello Again</p></div> -
parents( String expr ) returns jQuery
Get a set of elements containing the unique ancestors of the matched set of elements (except for the root element).
The matched elements can be filtered with an optional expression.
Example:
Find all parent elements of each span.
var parentEls = $("b").parents() .map(function () { return this.tagName; }) .get().join(", "); $("b").append("<strong>" + parentEls + "</strong>");HTML:
<div> <p> <span> <b>My parents are: </b> </span> </p> </div>Example:
Click to find all unique div parent elements of each span.
function showParents() { $("div").css("border-color", "white"); var len = $("span.selected") .parents("div") .css("border", "2px red solid") .length; $("b").text("Unique div parents: " + len); } $("span").click(function () { $(this).toggleClass("selected"); showParents(); });HTML:
<p> <div> <div><span>Hello</span></div> <span>Hello Again</span> </div> <div> <span>And Hello Again</span> </div> </p> <b>Click Hellos to toggle their parents.</b> -
password( ) returns Array<Element>
Matches all input elements of type password.
Example:
Finds all password inputs.
var input = $(":password").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 submitHTML:
<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> -
prepend( String, Element, jQuery content ) returns jQuery
Prepend content to the inside of every matched element.
This operation is the best way to insert elements inside, at the beginning, of all matched elements.Example:
Prepends some HTML to all paragraphs.
$("p").prepend("<b>Hello </b>");HTML:
<p>there friend!</p> <p>amigo!</p>Example:
Prepends a DOM Element to all paragraphs.
$("p").prepend(document.createTextNode("Hello "));HTML:
<p>is what I'd say</p> <p>is what I said</p>Example:
Prepends a jQuery object (similar to an Array of DOM Elements) to all paragraphs.
$("p").prepend( $("b") );HTML:
<p> is what was said.</p><b>Hello</b> -
prependTo( String content ) returns jQuery
Prepend all of the matched elements to another, specified, set of elements.
This operation is, essentially, the reverse of doing a regular $(A).prepend(B), in that instead of prepending B to A, you're prepending A to B.Example:
Prepends all spans to the element with the ID "foo"
$("span").prependTo("#foo"); // check prepend() examplesHTML:
<div id="foo">FOO!</div> <span>I have something to say... </span> -
prev( String expr ) returns jQuery
Get a set of elements containing the unique previous siblings of each of the matched set of elements.
Use an optional expression to filter the matched set.
Only the immediately previous sibling is returned, not all previous siblings.Example:
Find the very previous sibling of each div.
var $curr = $("#start"); $curr.css("background", "#f99"); $("button").click(function () { $curr = $curr.prev(); $("div").css("background", ""); $curr.css("background", "#f99"); });HTML:
<div></div> <div></div> <div><span>has child</span></div> <div></div> <div></div> <div></div> <div id="start"></div> <div></div> <p><button>Go to Prev</button></p>Example:
Find the very previous sibling of each paragraph that has a class "selected".
$("p").prev(".selected").css("background", "yellow");HTML:
<div><span>Hello</span></div> <p class="selected">Hello Again</p> <p>And Again</p> -
prevAll( String expr ) returns jQuery
Find all sibling elements before the current element.
Use an optional expression to filter the matched set.Example:
Locate all the divs before the last and give them a class.
$("div:last").prevAll().addClass("before");HTML:
<div></div> <div></div> <div></div> <div></div>