0

I have a page with a content accordion with 8 items. I also have an h4 tag on the page outside of the accordion. I want to hide which ever content accordion item matches the text inside the h4 tag.

The text inside the h4 tag and the content accordion items might change so I need to use variables (I think).

Here is what I have so far:

var category = $('.leftColumnNav h4').html();
var topic = $('.contentAccordionItemTitle p').html();

if(topic === category){
  $(".contentAccordionItemTitle").css("display", "none");
} else {
  $(".contentAccordionItemTitle").css("display", "block");
}

What I have sort of works. It successfully hides the .contentAccordionItemTitle. Unfortunately it obviously hides all of them. I just want to hide the one that matches the h4 tag.

If it's needed I can probably create a JSFiddle example.

2
  • 3
    Can you show your Html markup..? Commented May 15, 2013 at 20:41
  • 4
    "If it's needed I can probably create a JSFiddle example." It's usually a good idea to include a jsFiddle with questions like this. Commented May 15, 2013 at 20:41

3 Answers 3

1
var category = $('.leftColumnNav h4').text();

$(".contentAccordionItemTitle").each(function() {
  if ($(this).text() === category) { $(this).hide() }
})
Sign up to request clarification or add additional context in comments.

Comments

0
var topic = $('.contentAccordionItemTitle p').html();

That line means you're getting all the p-tags. If you want to continue down this solution, you could use the jQuery each function -> http://api.jquery.com/each/

      $(".contentAccordionItemTitle").css("display", "none");
  } else {
      $(".contentAccordionItemTitle").css("display", "block");

The $(".contentAccordionItemTitle") also gets all elements with this class.

Comments

0

You should use a loop, like jQuery each:

var category = jQuery('.leftColumnNav h4').html();
jQuery('.contentAccordionItemTitle p').each(function() {
    if(jQuery(this).html() === category) {
        jQuery(this).parent('.contentAccordionItemTitle').css('display', 'none');
    } else {
        jQuery(this).parent('.contentAccordionItemTitle').css('display', 'block');
    }

This is assuming there is only one element that matches jQuery('.leftColumnNav h4')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.