0

Trying to add a class to an element inside an li item when a function call is made. I can get the correct value outputted, however finding the child <i> is proving difficult. If I could find the correct nested <i> and add the class 'show' that would solve it :)

JS code:

filterMarkers = function(category) {
    for (i = 0; i < markers1.length; i++) {
        marker = gmarkers1[i];
        // If is same category or category not picked
        if (marker.category == category || category.length === 0) {
            marker.setVisible(true);
            // Show the tick icon
            $(".filter").find("[data-value='" + category + "']").addClass('show');

        }
        // Categories don't match
        else {
            marker.setVisible(false);
        }
    }
}

HTML code:

<ul class="drop-down">
    <li class="filter blue" data-value="" onclick="filterMarkers('');">All <i class="fi-check"></i></li>
    <li class="filter yellow" data-value="test-one" onclick="filterMarkers('test-one');">Sales <i class="fi-check"></i></li>
    <li class="filter red" data-value="test-two" onclick="filterMarkers('test-two');">Incentives <i class="fi-check"></i></li>
    <li class="filter grey" data-value="test-three" onclick="filterMarkers('test-three');">Conferences <i class="fi-check"></i></li>
    <li class="filter orange" data-value="test-four" onclick="filterMarkers('test-four');">Team building <i class="fi-check"></i></li>
</ul>
1
  • $(".filter[data-value='" + category + "']").addClass('show'); use this no need to use find since the class filter has the data attr Commented May 20, 2016 at 10:34

2 Answers 2

1

Looking at the filterMarkers method, you

  1. Either want to simply show the category which is passed as argument and hide everything else

  2. Or if no argument is passed then hide everything.

Simplify your code to

filterMarkers = function(category) {
    $(".filter").removeClass("show"); //remove show class from all
    if( category.length > 0 )
    {
       $(".filter[data-value='" + category + "']").addClass('show');
    }
    for (i = 0; i < markers1.length; i++) 
    {
        marker = gmarkers1[i];
        marker.category == category || category.length === 0 ? marker.setVisible( true ) : marker.setVisible( false );
        }
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

I only wish to add the class of show to the li item where the data-value matches the category :)
@JesseC and not hide the others? Please confirm. In that case, simply remove the first line in the method.
Yes, when selected add class 'show', remove from all others. The code you suggested isn't adding the class though, the find maybe the issue.
Thanks @gurvinder372 - it's working, marked as correct :)
0
$(".filter[data-value='" + category + "']").addClass('show');

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.