-2

I have been working on some toggle based buttons. I have two buttons one is buy and other is the demo. I have managed to write a bit of jQuery code for the toggle. everything is working fine except when I click on single button both of them are being toggled. But I want only one of it to be toggled. One other minor thing is when I click on the button some weird animation is being played I guess that is because I have used the toggle. Here is the Fiddle

$(document).ready(function($) {
  $('.theme-options').find('#buy').click(function() {
    $('.buy span').toggle(200);
  });

  $('.theme-options').find('#demo').click(function() {
    $('.demo span').toggle(200);
  });
});
body { position: relative; }
.theme-options {
  position: fixed;
  right: 0;
  top: 40px;
  display: flex;
  flex-direction: column;
  cursor: pointer;
}
.theme-options span {
  display: flex;
  align-items: center;
}
.buy,
.demo {
  padding: 20px 10px;
  display: block;
}
.buy span,
.demo span {
  display: none;
}
a {
  text-decoration: none;
  color: #000;
  text-transform: uppercase;
}
<div class="theme-options">
  <span id="buy">User
<a href="#" class="buy theme">
  <span>Want to buy this theme</span>
  </a>
  </span>
  <span id="demo">User
<a href="#" class="demo theme">
  <span>Want to request a demo</span>
  </a>
  </span>
</div>
Mohammed Wahed Khan
  • 836
  • 2
  • 14
  • 35
  • I dont see what you mean. When I click the first button, the first button toggles. When I click the second button, the second button toggles. No idea what you mean with "both of them are being toggeled" – Tim Gerhard Mar 28 '18 at 06:10
  • When you click on the first button the second button is also being toggled but not showing content. I want the send button to stay in the default place. – Mohammed Wahed Khan Mar 28 '18 at 06:12
  • 2
    No it is not. The thing is, your "animation" that shows the content pushes the buttons away from the right side. The problem lies on your animation. not the toggling itsself – Tim Gerhard Mar 28 '18 at 06:14
  • @TimGerhard Can you help me on that? I have only bit of knowledge regarding JQuery – Mohammed Wahed Khan Mar 28 '18 at 06:15
  • It's a CSS issue Mohammed. Not a jQuery one. I may help you let me check... – Santiago Baigorria Mar 28 '18 at 06:20
  • Well of course I could help but I dont know how your animation is supposed to look – Tim Gerhard Mar 28 '18 at 06:21
  • @TimGerhard It is just like two icons on the top right in fixed position. When you click on the icon the text rolls out from right to left.one can buy the theme or request demo. but I want this thing in mobile so I am not applying any hover effect. – Mohammed Wahed Khan Mar 28 '18 at 06:25

4 Answers4

1

It is due to theme-options class taking width which is applying to both element so both will move on on toggle you can seperate them in different div and apply this

$(document).ready(function($) {
    $('.theme-options').find('#buy').click(function(){

      $('.buy span').toggle(500);

    });
    
      $('.theme-options2').find('#demo').click(function(){

      $('.demo span').toggle(500);

    });
  });
body {
  position: relative;
}
.theme-options {
  position: fixed;
  right: 0px;
  top: 40px;
  display: flex;
  flex-direction: column;
  cursor: pointer;
}
.theme-options span {
  display: flex;
  align-items: center;
}
.theme-options2 {
  position: fixed;
  right: 0px;
  top: 80px;
  display: flex;
  flex-direction: column;
  cursor: pointer;
}
.theme-options2 span {
  display: flex;
  align-items: center;
}
.buy, .demo {
  padding: 20px 10px;
  display: block;
}
.buy span {
 display: none;
}
.demo span {
 display: none;
}
a {
  text-decoration: none;
  color: #000;
  text-transform: uppercase;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="theme-options">
<span id="buy">User
<a href="#" class="buy theme">
  <span>Want to buy this theme</span>
  </a>  
</span>
</div>
<div class="theme-options2">
<span id="demo">User
<a href="#" class="demo theme">
  <span>Want to request a demo</span>
  </a>  
</span>
</div>
Hiren Vaghasiya
  • 5,454
  • 1
  • 11
  • 25
1

CSS

body {
    position: relative;
}

.theme-options {
    position: fixed;
    height: 40px;
    right: 0;
    top: 40px;
    display: flex;
    flex-direction: column;
    cursor: pointer;
}

    .theme-options span {
        display: flex;
        align-items: center;
    }

.theme-options1 {
    position: fixed;
    height: 40px;
    right: 0;
    top: 80px;
    display: flex;
    flex-direction: column;
    cursor: pointer;
}

    .theme-options1 span {
        display: flex;
        align-items: center;
    }

.buy, .demo {
    padding: 20px 10px;
    display: block;
}

    .buy span, .demo span {
        display: none;
    }

a {
    text-decoration: none;
    color: #000;
    text-transform: uppercase;
}

JS

$(document).ready(function ($) {
    $('.theme-options').find('#buy').click(function () {
        $('.buy span').toggle(200);
    });

    $('.theme-options1').find('#demo').click(function () {
        $('.demo span').toggle(200);
    });
});

HTML

<div class="theme-options">
    <span id="buy">User
        <a href="#" class="buy theme">
            <span>Want to buy this theme</span>
        </a>
    </span>
</div>
<div class="theme-options1">
    <span id="demo">User
        <a href="#" class="demo theme">
            <span>Want to request a demo</span>
        </a>
    </span>
</div>

Fiddle

Waleed Iqbal
  • 1,308
  • 19
  • 35
1

You can Try this way

$('#buy,#demo').click(function(){
        $(this).find('span').fadeToggle(300);
    });
body {
  position: relative;
}
.theme-options {
  position: fixed;
  right: 0;
  top: 40px;
  display: flex;
  flex-direction: column;
  cursor: pointer;
}
.theme-options:nth-of-type(2) {
 top: 90px;
}
.theme-options span {
  display: flex;
  align-items: center;
}
.buy, .demo {
  padding: 20px 10px;
  display: block;
}
.buy span, .demo span {
 display: none;
}
a {
  text-decoration: none;
  color: #000;
  text-transform: uppercase;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="theme-options">
<span id="buy">User
<a href="#" class="buy theme">
  <span>Want to buy this theme</span>
  </a>  
</span>

</div>
<div class="theme-options">
<span id="demo">User
<a href="#" class="demo theme">
  <span>Want to request a demo</span>
  </a>  
</span>
</div>
Athul Nath
  • 2,536
  • 1
  • 15
  • 27
1
    $(document).ready(function($) {
      $('.theme-options').find('#buy').click(function(){

        $('.buy span').toggle(200);
        if($('.demo span').css('display') == 'inline'){
          $('.demo span').toggle(200);
        }

      });

        $('.theme-options').find('#demo').click(function(){

        $('.demo span').toggle(200);
        if($('.buy span').css('display') == 'inline'){
          $('.buy span').toggle(200);
        }
      });
    });

is it what you want? you need to judge whether another item is expand

suedar
  • 130
  • 10