0

I am trying to add class on my li element.I want to add class on click. when I click on button its add css class on current li . Like when I click first time then it's active first time. after second click it's active on second li,

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $("li:first").addClass("intro");
    });
});
</script>
<style>
.intro {
    font-size: 150%;
    color: red;
}
</style>
</head>
<body>

<h1>This is a heading</h1>

<li>This is a heading</li>
<li>This is a heading</li>
<li>This is a heading</li>
<li>This is a heading</li>
<li>This is a heading</li>
<li>This is a heading</li>
<li>This is a heading</li>
<li>This is a heading</li>


<button>Add a class name to the first p element</button>

</body>
</html>
3
  • Possible duplicate of just about everything Commented May 6, 2017 at 7:47
  • What should be your active element? I see no .active or anything in your code Commented May 6, 2017 at 7:47
  • What should happen clicking the button 10 times? Commented May 6, 2017 at 7:54

3 Answers 3

1

Count the click event and apply with eq() .And li length is 8 .so apply condition after 7 reach ,count reset to 0

var c = 0;
$(document).ready(function() {
  $("button").click(function() {
    if (c > 7) {
      c = 0;
    }
    $("li").removeClass('intro')
    $("li").eq(c).addClass("intro");
    c++;

  });
});
.intro {
  font-size: 150%;
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<body>

  <h1>This is a heading</h1>

  <li class="intro">This is a heading</li>
  <li>This is a heading</li>
  <li>This is a heading</li>
  <li>This is a heading</li>
  <li>This is a heading</li>
  <li>This is a heading</li>
  <li>This is a heading</li>
  <li>This is a heading</li>


  <button>Add a class name to the first p element</button>

</body>

</html>

Updated answer with prev and next for slide

var c = 0;
$(document).ready(function() {
  $("button").click(function() {
    $(this).attr('data') == 'next' ? c++ : c--;
    if (c > 7) {
      c = 0;
    }
    $("li").removeClass('intro')
    $("li").eq(c).addClass("intro");


  });
});
.intro {
  font-size: 150%;
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<body>

  <h1>This is a heading</h1>

  <li class="intro">This is a heading</li>
  <li>This is a heading</li>
  <li>This is a heading</li>
  <li>This is a heading</li>
  <li>This is a heading</li>
  <li>This is a heading</li>
  <li>This is a heading</li>
  <li>This is a heading</li>


  <button data="next">next</button>
  <button data="prev">prev</button>

</body>

</html>

Sign up to request clarification or add additional context in comments.

7 Comments

Prasad, c = c > 7 ? 0 : c;... we're not supposed to modify JS by adding a HTML element. Can you improve your answer to make your JS more dynamic?
prasad@ you there
Prasad , I need first Li as a active class,
@sanjay for default first li is active right.see my updated answer
Well this is awesome @ Thanks for this
|
0

Maybe you could do it like this:

$(document).ready(function(){

    $("button").click(function(){
       //Find the li with the active class and add your other class
       $("li.active").addClass("your-class");
    });
})

Here is a demo snippet

$(document).ready(function(){

  $("button").click(function(){
    $("li.active").addClass('foo-class')
  
  })
})
.foo-class{
  color:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li class='active'>Active</li>
  <li> Non active</li>
</ul>
<button>Click me</button>

7 Comments

Why downvoting?Can you explain
why?Can you explain
@RokoC.Buljan have a look at my snippet
Roko C.Buljain@ if you have good amount of connect it's not mean you have to previlage to down any script or question
@sanjay thanks .My scrip may not do what the OP wants but it is a working code
|
0

Find the li with the active class and add your other class

$(document).ready(function(){
    $("button").click(function(){
       // Find li and add other class
       var l=$("body").find("li:first-child");
       $(l).addClass("your-class");
    });
})

1 Comment

Thanks To Improve my Answer.

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.