In this article, We will learn how to add or remove class using data attributes in jquery after a click on the button.
Please follow these steps:
<a class="add_button button" data-id="test1" href="#">Add Class</a> <a class="remove_button button" data-id="test1" href="#">Remove Class</a>
<div class="test1"></div> <p class="test1"></p>
var id = $(this).data("id");
$("." + id).addClass("active"); $("." + id).removeClass("active");
Here is a full example:
HTML:
<a class="add_button button" data-id="test1" href="#">Add Class</a> <a class="remove_button button" data-id="test1" href="#">Remove Class</a> <div class="test1"></div> <p class="test1"></p> <a class="add_button button" data-id="test2" href="#">Add Class</a> <a class="remove_button button" data-id="test2" href="#">Remove Class</a> <div class="test2"></div> <p class="test2"></p> <a class="add_button button" data-id="test3" href="#">Add Class</a> <a class="remove_button button" data-id="test3" href="#">Remove Class</a> <div class="test3"></div> <p class="test3"></p>
JQUERY:
$(".add_button").click(function( event ){ event.preventDefault(); var id = $(this).data("id"); $("." + id).addClass("active"); }); $(".remove_button").click(function( event ){ event.preventDefault(); var id = $(this).data("id"); $("." + id).removeClass("active"); });
Output:
In this article, we have to show Create and Used PIPE in angular
In this article, we have to show Create and Used PIPE in angular
In this article, we have to show Create and Used PIPE in angular