Add and Remove Class in jQuery on Click

Add and Remove Class in jQuery on ClickIn this example we will show add and remove class in jquery on click. I explain step by step add and remove class name in jquery. In this example you learn how to add and remove a class in jquery.

We will explain example of add remove class jquery using single button. single button to you can add class and remove class. I will show add and remove class using jquery.

Add and remove class on button click jQuery

Example

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Add and Remove Class in jQuery on Click</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
  <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/all.min.css" rel="stylesheet">

</head>
<body>

<div class="container">
    <table class="table table-bordered">
        <thead>
            <tr>
                <th>Firstname</th>
                <th>Lastname</th>
                <th>Email</th>
                <th>Action</th>
            </tr>
        </thead>
        <?php $id = 1; ?>
        <tbody>
            <tr class="tr_<?= $id ?>">
                <td><input type="text" id="firstname" value="John" class="form-control" disabled></td>
                <td><input type="text" id="lastname" value="Doe" class="form-control" disabled></td>
                <td><input type="email" id="email" value="john@example.com" class="form-control" disabled></td>
                <td>
                    <button class="btn btn-primary btn_edit" data-id="<?= $id ?>"><i class="fas fa-edit"></i></button>
                </td>
            </tr>
        </tbody>
    </table>
</div>

</body>
<script>
    $(document).ready(function(){
        $(document).on("click",".btn_edit",function(){
            alert("Edit Action");
            var id = $(this).attr("data-id");
            $(".tr_"+id).find("input").attr("disabled",false);
            $(this).addClass("btn_save");
            $(this).removeClass("btn_edit");
            $(this).find("i").removeClass("fa-edit");
            $(this).find("i").addClass("fa-save");
            return false;
        });
        
        $(document).on("click",".btn_save",function(){
            alert("Save Action");
            var id = $(this).attr("data-id");
            $(".tr_"+id).find("input").attr("disabled",true);
            $(this).removeClass("btn_save");
            $(this).addClass("btn_edit");
            $(this).find("i").removeClass("fa-save");
            $(this).find("i").addClass("fa-edit");
            return false;
        });
    });

</script>
</html>

I hope you understand of add and remove class in jquery using on button click and it can help you…