0

I code this in HTML / CSS and Jquery :

$('.myFilters li').click(function() {
    $(".category").hide();
    var v = $(this).text()[0]
    $('.title li').hide().filter(function() {
        return $(this).text().toUpperCase()[0] == v;
        $(".category:first").show();
    }).show().first().find('a[data-toggle]:first').trigger('click');
})

$("a[data-toggle]").on("click", function(e) {
    e.preventDefault(); // prevent navigating
    var selector = $(this).data("toggle"); // get corresponding element
    $(".category").hide();
    $(selector).show();
});


$('.myFilters li:first').trigger('click');
//$('.title li:first a[data-toggle]').trigger('click');
.myFilters,
.title {
	margin: 0;
	padding: 0;
}

.myFilters {
  display:inline-block;
	margin-right: 10px;
	margin-bottom: 15px;
}

.myFilters li {
  display:inline-block;
	margin-right: 10px;
}

.myFilters li a {
	font-size: 22px;
	font-style: normal !important;
	font-weight: bold;
  text-decoration: none;
  color: black;
}

#menu-navigation > li:first-child{
   color:red;
}

.title {
	margin-bottom: 30px;
}

.title li {
	display: inline-block;
	list-style-type: none;
	padding: 15px;
	background-color: white;
	border: 1px solid #d6205c;
	color: white;
    margin-right: 15px;
    margin-bottom: 10px;
    padding: 0;
}

.title li a {
    display: block !important;
    color: #d6205c;
    padding: 5px;
    font-style: normal !important;
    margin: 0 !important;
}


.category {
	margin-bottom: 25px;
	margin-top: 40px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul class="myFilters">
    <li data-type="A"><a href="#">A</a></li>
    <li data-type="B"><a href="#">B</a></li>
    <li data-type="C"><a href="#">C</a></li>
</ul>
<div class="filter">
    <ul class="title">
        <li><a href="#" data-toggle="#Assurance">Assurance</a></li>
        <li><a href="#" data-toggle="#Couverture">Couverture</a></li>
        <li><a href="#" data-toggle="#Banque">Banque</a></li>
        <li><a href="#" data-toggle="#Alimentation">Alimentation</a></li>
    </ul>
    <div id="Assurance" class="category">
        <ul>
            <li>Groupama</li>
        </ul>
    </div>
    <div id="Couverture" class="category">
        <ul>
            <li>Try it !</li>
        </ul>
    </div>
    <div id="Alimentation" class="category">
        <ul>
            <li>AN example</li>
        </ul>
    </div>
</div>

When you click on a letter, categories appears and its companies.

What I want is when you click on a letter, this letter changes its color.

And the same for the categories, WHen you click on a category, its background changes.

Thanks for the help !

1

3 Answers 3

1

Try this fiddle

$('.myFilters li').click(function() {
    $('.myFilters li').find('a').removeClass('red');
    $(this).find('a').addClass('red');
    $(".category").hide();
    var v = $(this).text()[0]
    $('.title li').hide().filter(function() {
        return $(this).text().toUpperCase()[0] == v;
        $(".category:first").show();
    }).show().first().find('a[data-toggle]:first').trigger('click');
})

$("a[data-toggle]").on("click", function(e) {
    e.preventDefault(); // prevent navigating
    var selector = $(this).data("toggle"); // get corresponding element
    $(".category").hide();
    $(selector).show();
});


$('.myFilters li:first').trigger('click');
//$('.title li:first a[data-toggle]').trigger('click');
Sign up to request clarification or add additional context in comments.

3 Comments

That's simple. Thanks for this reply ;)
I'm trying to do the same for the li from .title jsfiddle.net/6eqabkjr/1 but it does not work
Replace your second line with this and check, $(this).css('background-color', '#d6205c'); :)
1

Something like this might work for you http://jsfiddle.net/wdg9khL5/ ?

$('.myFilters li').click(function() {
  $(".category").hide();
  var v = $(this).text()[0];
  var c = $(this).data("color");
  $('.myFilters li a').css("color", "black");
  $("a", this).css("color", c);
  $('.title li').hide().filter(function() {
      return $(this).text().toUpperCase()[0] == v;
      $(".category:first").show();
  }).show().first().find('a[data-toggle]:first').trigger('click');
})

$("a[data-toggle]").on("click", function(e) {
  e.preventDefault(); // prevent navigating
  var selector = $(this).data("toggle"); // get corresponding element
  $(".category").hide();
  $(selector).show();
});


$('.myFilters li:first').trigger('click');

$('.filter > ul > li > a').on("click", function(e){
   // Reset all link colors
   $('.filter > ul > li > a').css("color",  'red');
   $('.filter > ul > li').css("border", '1px solid red');

   $(this).css("color",  'green');
   $(this).parent('li').css("border", '1px solid green');
});

Comments

0

You can create a data-color attribute. And try this:

$('.myFilters li').on("click", function(e) {
    e.preventDefault(); // prevent navigating
    //...
    var color = $(this).data("color");
    $("a", this).css({color: color});
});

Fiddle

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.