Having problem with andSelf() while reading O'Reillys jQuery Cookbook

mongoose00318 :

I am trying to get refreshed on jQuery so I bought O'Reillys jQuery cookbook. I'm having trouble with the exercise on using andSelf() to manipulate the parent of the selected element; though my code is exactly like the books.

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Untitled Document</title>
</head>

<body>

    <div>
        <p>Paragraph</p>
        <p>Paragraph</p>
    </div>

    <script
      src="https://code.jquery.com/jquery-3.4.1.js"
      integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
      crossorigin="anonymous">
    </script>
    <script>
        $('div').find('p').andSelf().css('border', '1px solid #993300');
    </script>
</body>
</html>

From my understanding my jQuery should add a border to the div but it doesn't. When I remove andSelf() it does add a border to the 2 p tags though. Am I doing something wrong?

N'Bayramberdiyev :

In the jQuery API Documentation:

This API has been removed in jQuery 3.0; use .addBack() instead, which should work identically.

$(function() {

    $('div').find('p').addBack().css('border', '1px solid #993300');

});
<div>
    <p>Paragraph</p>
    <p>Paragraph</p>
</div>

<script src="https://code.jquery.com/jquery-3.4.1.js"></script>

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=376528&siteId=1