Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to toggle two buttons on mouse hover?

Tags:

html

jquery

css

I've two buttons:
1 - Follow
2 - Unfollow

At first glance the Follow button will be displayed and then when you hover over the Follow button, then I want to make the follow button to disappear and Unfollow button to appear, then after once hovered over the Unfollow button then I want the Follow button to appear and Unfollow button to dispear, so I how do I do it?

Feed backs are welcomed.

HTML CODE

<div class="btn-follow">Follow</div>
<div class="btn-unfollow">Unfollow</div>

CSS CODE

.btn-follow {
    color: #FFF;
    background-color: #38B7EA;
    padding: 5px 0;
    width: 100px;
    margin: 0 auto;
    border-radius: 20px;
}
.btn-unfollow {
    color: #FFF;
    background-color: #A5BECB;
    padding: 5px 0;
    width: 100px;
    margin: 0 auto;
    border-radius: 20px;
}
like image 216
Firnas Avatar asked May 14 '15 06:05

Firnas


People also ask

What are hover buttons?

Hoverable Buttons Use the :hover selector to change the style of a button when you move the mouse over it.

How do I toggle between buttons in JavaScript?

A tgl() method is utilized to toggle a button in JavaScript. In this method, you extract the HTML element by employing the getElementById property, and then the if else-if statement is added to it. If the “value==ON”, toggle the value to “OFF”. If the value is OFF, then the value will be toggled to “ON”.


3 Answers

An alternative and more optimal solution as I said which can be done for same button as below:

DEMO HERE

HTML

<button id="followUnFollow" class="followUnF follow">Follow</button>

JS

$(document).ready(function(){
       $('#followUnFollow').on('click',function()
       {
           if($(this).hasClass('follow'))
               $(this).removeClass('follow').addClass('unfollow').text('Unfollow');
           else
               $(this).removeClass('unfollow').addClass('follow').text('Follow');           
       });
});

UPDATE

If you want the same on hover instead of click you can change .on('click' to hover as below:

$('#followUnFollow').hover(function(){
  ......
});
like image 71
Guruprasad J Rao Avatar answered Oct 22 '22 20:10

Guruprasad J Rao


Try following code:

It will hide the clicked button and show other button.

$('.btn-follow').on('click', function () {
    $(this).hide();
    $('.btn-unfollow').show();
});
$('.btn-unfollow').on('click', function () {
    $(this).hide();
    $('.btn-follow').show();
});

Demo: https://jsfiddle.net/tusharj/Ls8gk2es/

like image 45
Tushar Avatar answered Oct 22 '22 21:10

Tushar


I think using hover event you can make it simpler.

HTML

<button class="btn btn-follow">Follow</button>
<button class="btn btn-unfollow">Unfollow</button>

JS

$('.btn-unfollow').hide();
$('.btn').hover(function () {
    $('.btn').toggle();
});

CSS (same as the styling given in your question)

like image 1
Lini Susan V Avatar answered Oct 22 '22 20:10

Lini Susan V