jQuery

[jQuery] jQuery/Method/.addBack() - method to select the currently selected element as well as the previously selected element

OOQ 2022. 8. 9. 09:07
728x90
SMALL

#jQuery

#jQuery/Method/.addBack() - method to select the currently selected element as well as the previously selected element

.addBack()

.addBack() allows the previously selected element to be selected along with the currently selected element.

Grammar

.addBack( [selector ] )

for example

$( 'ul' ).find( 'li' ).addBack()

selects li among the children of ul, and selects the first selected ul.

example

Inside the div element find the p element with class value ip and select it, then select the div element as well to create the border.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>jQuery</title>
    <style>
      .abc {
        padding: 5px;
        border: 1px solid #444444;
      }
    </style>
    <script src="//code.jquery.com/jquery-3.3.1.min.js"></script>
    <script>
      $( document ).ready( function() {
        $( 'div' ).find( 'p.ip' ).addBack().addClass( 'abc' );
      } );
    </script>
  </head>
  <body>
    <div>
      <p>Lorem</p>
      <p class="ip">Ipsum</p>
      <p>Dolor</p>
    </div>
  </body>
</html>

TIP.
Use .add() if you have selected an element and want to select another element.

728x90
LIST