Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript " getParent() " not working

I have to hide my parent span just above <a> tag and show the span just below it.(When clicking it on the <a> tag).

I need to use getParent() because their are another same <div>s repeating

My HTML is

<div class="Test">
  <p class="commentBody">
    <span class="view_more-comment">abcd...
      <a class="view_more_link" href="javascript:void(0);" onclick="$(this).getParent().getNext().style.display='';$(this).getParent().style.display='none';">more</a>
    </span>
    <span class="view_more" style="display: none;">abcdefghijklmnopqrstuvwzyz...
      <a class="view_less_link" onclick="$(this).getParent().getPrevious().style.display='';$(this).getParent().style.display='none';">less</a>
    </span>
  </p>
</div>

When I use this console shows the error

Uncaught TypeError: $(...).getParent is not a function

Any method to solve this problem?.

like image 394
Vishnu Prasad Avatar asked May 02 '26 11:05

Vishnu Prasad


1 Answers

There is no getParent() in jQuery. Use:

onclick="$(this).parent().prev().show(); $(this).parent().hide();"

Docs

Get the parent of each element in the current set of matched elements, optionally filtered by a selector.

I'd recommend you to use on for event handling and not inline handlers

EDIT

Using jQuery:

$('.view_less_link').on('click', function() {
    $(this).closest('.commentBody').find('.view_more-comment').show();
    $(this).parent().hide();
});
like image 120
Tushar Avatar answered May 04 '26 00:05

Tushar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!