Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show elements depending on html value of a tag

I would like to accomplish the following with jquery :

When I click on this link

<a href="#">Cars</a>

I would like all divs like those

<div class="product">
    <div class="category">Cars</div>
</div>

to do something.

You get the idea, I have a menu with a list of categories, and a list of products, each containing a div with the category name, and I would like to make them hide/show.

like image 954
mike23 Avatar asked Jan 20 '26 01:01

mike23


2 Answers

I am not sure if i fully understand your question, but if you want the div class="category" cars to appear when cars link is clicked, follow this:

$("#menu a").click(function() {
   var value = $(this).html();

   $.each($(".category"), function(i, item) {
     if ($(item).html() == value) {
         $(item).parent().hide();
     }

   });
});

if you want to hide the div just replace $(item).show(); with $(item).hide();

like image 182
Germán Rodríguez Avatar answered Jan 21 '26 15:01

Germán Rodríguez


Assuming:

<a href="#" class="highlight">Cars</a>

then:

$("a.highlight").click(function() {
  $("div.category").removeClass("category")
    .filter(":contains(" + $(this).text() + ")").addClass("highlight");
  return false;
});

What this does is add the category class to any category dvis that contain the text of the link. This can be modified to modify the parent product div if you want to do it that way too.

It works by first removing the class highlight from all category divs and then adding it to the ones that require it.

like image 25
cletus Avatar answered Jan 21 '26 15:01

cletus



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!