Why the list item dont stay active when is clicked?
I have a menu with some categories:
- <ul class="Categories__Menu">
- @foreach($categories->take(6) as $category)
- <li class="active">
- <a href="#" name="category" id="{{$category->id}}" href="#">{{$category->name}}</a>
- </li>
- @endforeach
- <li><a data-toggle="modal" id="showCategories" data-target=".bd-example-modal-lg" href="">More</a></li>
- </ul>
If the user clicks in More it opens a modal with more categories, the modal have this content:
- <div class="modal fade bd-example-modal-lg" id="categoriesModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
- <div class="modal-dialog modal-lg">
- <div class="modal-body">
- <div class="container">
- <div class="row">
- <ul class="modal-list row">
- <li class="col-lg-4 col-md-6 col-sm-12">
- <a class="">All Categories</a>
- </li>
- @foreach($categories as $category)
- <li class="col-lg-4 col-md-6 col-sm-12">
- <img src="{{ $category->image }}"/>
- <a name="category" id="{{$category->id}}">{{$category->name}}</a>
- </li>
- @endforeach
- </ul>
- </div>
- </div>
- </div>
- </div>
- </div>
I have some jquery to do an ajax request to get the conferences that belong to the clicked category. This is working fine.
But I want that the clicked category becomes active in the menu "Categories__Menu". But its not working, the clicked category don't stays active.
This fiddle "
http://jsfiddle.net/e7gjv5ct/6/" demonstrates the issue. The item doesn't stays with the class active when is clicked. And also the item "More" changes the text from "More" to the clicked item text, but the item "More" should only change the text to the clicked category text when a category in the modal is clicked, that is, it should not change when a category in the menu "Categories__Menu" is clicked.
Do you know where is the issue?
- $("a[name='category']").on('click', function(){
- $('#showCategories').html($(this).text()+' <i class="fa fa-caret-down" aria-hidden="true"></i>');
- $('#categoriesModal').modal('hide');
- $('#showCategories').addClass('active');
-
- var category_id = $(this).attr("id");
- //alert(category_id);
-
- $('.Categories__Menu li').removeClass('active');
- $(this).parent('li').addClass('active');
-
- $.ajax({
-
- url: '{{ route('category.conferences',null) }}/' + category_id,
- type: 'GET',
- success:function(result){
- $('#conferences').empty();
- var newConferences='';
- var placeholder = "{{route('conferences.show', ['id' => '1', 'slug' => 'demo-slug'])}}";
- $.each(result, function(index, conference) {
- var url = placeholder.replace(1, conference.id).replace('demo-slug', conference.slug);
-
- newConferences += '<div class="col-12 col-sm-6 col-lg-4 col-xl-3 mb-4">\n' +
- ' <div class="card box-shaddow">\n' +
- ' <img class="card-img-top" src='+ conference.image +' alt="Card image cap">\n' +
- ' <div class="card-body">\n' +
- ' <h5 class="card-title">'+conference.name+'</h5>\n' +
- ' </div>\n' +
- ' </div></div>';
- });
- $('#conferences').html(newConferneces);
- },
- error: function(error) {
- console.log(error.status)
- }
- });
- });