prev

UK[prɪv] 美[prɪv]

[medical]Prevention

until

英[ənˈtɪl] 美[ʌnˈtɪl]

prep.Until...;before...

conj.Until..., before...;until...

jquery prevUntil() method syntax

Function: prevUntil() method obtains the previous sibling elements of each element in the current matching element set, but does not include elements matched by selectors, DOM nodes or jQuery objects.

Syntax 1: .prevUntil(selector, filter)

##Parameters:

ParameterDescriptionselector Optional. A string value containing a selector expression indicating where to stop matching previous sibling elements. filter Optional. String value containing the selector expression used to match the element.

Syntax 2: .prevUntil(element, filter)

Parameters:

ParametersDescriptionelement Optional. A DOM node or jQuery object indicating where to stop matching preceding sibling elements. filter Optional. String value containing the selector expression used to match the element.

Description: If given a jQuery object that represents a collection of DOM elements, the .prevUntil() method allows us to search in front of these elements in the DOM tree sibling elements until an element matched by the selector (the parameter passed to the method) is encountered. The new jQuery object returned contains all previous sibling elements except the one matched by the selector specified by the .prevUntil() method; the returned elements are ordered from the nearest sibling to the furthest one. If there is no match or no selector is applied, all preceding sibling elements will be selected; in this case, the method selects the same elements as .prevAll() if no selector is provided. As of jQuery 1.6, a DOM node or jQuery object, instead of a selector, can be used as the first argument to the .prevUntil() method. This method accepts an optional selector expression as its second parameter. If this parameter is applied, elements will be filtered by testing whether they match this selector.

jquery prevUntil() method example

<!DOCTYPE html>
<html>
<head>
  <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
</head>

<body>
<dl>
  <dt id="term-1">term 1</dt>
  <dd>definition 1-a</dd>
  <dd>definition 1-b</dd>
  <dd>definition 1-c</dd>
  <dd>definition 1-d</dd>

  <dt id="term-2">term 2</dt>
  <dd>definition 2-a</dd>
  <dd>definition 2-b</dd>
  <dd>definition 2-c</dd>

  <dt id="term-3">term 3</dt>
  <dd>definition 3-a</dd>
  <dd>definition 3-b</dd>
</dl>

<script>
$("#term-2").prevUntil("dt")
  .css("background-color", "red");
  
var term1 = document.getElementById('term-1');
$("#term-3").prevUntil(term1, "dd")
  .css("color", "blue");
</script>

</body>
</html>
Run instance »

Click the "Run instance" button to view the online instance