1

I followed this jQuery: Hide parent <td> if contains child <input> with specific class? but its not working in my code:

$(function() {
$(".video-list-item").filter(function() {
     return $('span', this).hasClass('.view-count');
}).hide();
});

https://jsfiddle.net/xzeeuotL/

2
  • Please describe what exactly you're trying to achieve Nov 4, 2016 at 14:02
  • hasClass('view-count')
    – JonSG
    Nov 4, 2016 at 14:03

6 Answers 6

3

Two things to fix but you were close.

1) You did not include Jquery as a library for the JSFiddle

2) In the hasClass method you need to remove the . in the class name

$(function() {
$(".video-list-item").filter(function() {
     return $('span', this).hasClass('view-count');
}).hide();
});

Here is a working fiddle

https://jsfiddle.net/rn2jzpfc/

1

You were close. Just one mistake in your code. When you are checking if something hasClass there is no need to have the . preface it.

Also remember you need to include jquery.

$(function() {
  $(".video-list-item").filter(function() {
    return $('span', this).hasClass('view-count');
  }).hide();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="video-list-item">
  <span class="view-count">HIDE THIS</span>
</div>
<div class="video-list-item">
  <span>DON'T HIDE THIS</span>
</div>

You can do this without Jquery

Here is an example using pure javascript.

var divSel = document.querySelectorAll('.video-list-item > .view-count');

for (var i = 0; i < divSel.length; i++) {
  divSel[i].parentElement.style.display = "none";
}
<div class="video-list-item">
  <span class="view-count">HIDE THIS</span>
</div>
<div class="video-list-item">
  <span>DON'T HIDE THIS</span>
</div>

Hope this helps.

0

The next code returns the parent with video-list-item class of elements with view-count class; then hide the parent. I thinks it's more easy than your approach:

$(".view-count").closest('.video-list-item').hide();


Updated JSFiddle: https://jsfiddle.net/tomloprod/xzeeuotL/2/

0

Another solution:

$(function() {
    $('.video-list-item').find('span.view-count').hide();
});
0

You have to change that syntax for your case.

you have to be sure that your <td> has a class name like ".video-list-item" and later change in the $('span', this) for $('input', this), of course, you input would has a class name like view-count. something like this..

$(function() {
$(".video-list-item").filter(function() {
     return $('input', this).hasClass('view-count');
}).hide();
});


<td class="video-list-item">
 <input class="view-count">
</td>

i hope it can help you.. nice day!!

0

Useful thing if you want to remove closest parent that contains child with specific class:

<div class="closest_parent">
    <div></div>
    <div class="child_class"></div>
</div>
$('.child_class').closest('.closest_parent').remove();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.