user944513
user944513

Reputation: 12729

why parent is not working in jquery?

could you please tell me why parent function is not working properly ?

here is my code

https://jsbin.com/gefacenefi/edit?html,js,output

 $(function () {

        $('.add').click(function () {
            $('.item').parent('li.nn').css('color', 'red');
        });

    });

Expected output this text to be red

<li class="nn">bhiooo</li>

Upvotes: 0

Views: 627

Answers (4)

selvarajmas
selvarajmas

Reputation: 1643

Try the following code

$('.add').click(function () {
  $('.item').parents().find('.nn').css('color', 'red');
});

Here is the working jsfiddle:https://jsfiddle.net/nq9tzafz/

I think it should helps you

Upvotes: 0

Muhammad Qasim
Muhammad Qasim

Reputation: 1722

Please do the following:

$(function () {
    $('.add').click(function () {
      $('.item').parent().siblings('li.nn').css('color', 'red');
    });
});

Also you are not closing your li element properly. The structure should be like following:

<!DOCTYPE html>
<html>
<head>
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width">
   <title>JS Bin</title>
   <script src="https://code.jquery.com/jquery-2.1.4.js"></script>
</head>
<body>
   <button class="add">filler</button>
   <div>
      <ul>
         <li class="nn">bhiooo</li>
         <li class="m">hello22
            <ul class="item">
                <li class="abc">123</li>
                <li class="pp">12</li>
                <li class="abc">78</li>
                <li class="ac">13</li>
                </li>
            </ul>
         </li>
      </ul>
   </div>
</body>
</html>

Upvotes: 0

Dan Philip Bejoy
Dan Philip Bejoy

Reputation: 4376

You are trying to call a parent of a different node. Try this

$('.add').click(function () {
  $('.item').parent().siblings('li.nn').css('color', 'red');
});

Upvotes: 1

Cyril
Cyril

Reputation: 401

Your ul .item is not under .nn.

Change the HTML, or adapt you JS

$('.item').parent().prev('li.nn').css('color', 'red');

Upvotes: 0

Related Questions