-1

I have a row of buttons:

 <div class="console_row1">
            <button class="button">TOFU</button>
            <button class="button">BEANS</button>
            <button class="button">RICE</button>
            <button class="button">PASTA</button>
            <button class="button">QUINOA</button>
            <button class="button">SOY MILK</button>
</div>

And I'm trying to make a program where if you click on one of the buttons, it ONLY fades out the button you clicked on. I have the following Jquery:

 $(document).ready(function(){
            $(".button").click(function(){
                $(".button").fadeOut()    
            });    
        });

1 Answers1

3

Use $(this).this will give the reference to the member that invokes the current function.You can Read more here

 $(document).ready(function(){
            $(".button").click(function(){
                $(this).fadeOut()    
            });    
        });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="console_row1">
            <button class="button">TOFU</button>
            <button class="button">BEANS</button>
            <button class="button">RICE</button>
            <button class="button">PASTA</button>
            <button class="button">QUINOA</button>
            <button class="button">SOY MILK</button>
</div>
And I'm trying to make a program where if you click on one of the buttons, it ONLY fades out the button you clicked on. I have the following Jquery:

For fadeout using css

 $(document).ready(function(){
                $(".button").click(function(){
                    $(this).addClass('fadeOut')    
                });    
            });
.button{
  transition:all .5s linear;
}

.fadeOut{
  opacity:0;
}
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="console_row1">
                <button class="button">TOFU</button>
                <button class="button">BEANS</button>
                <button class="button">RICE</button>
                <button class="button">PASTA</button>
                <button class="button">QUINOA</button>
                <button class="button">SOY MILK</button>
    </div>
 
 
XYZ
  • 4,450
  • 2
  • 15
  • 31