2

I want to toggle some inline CSS with a jQuery script, but I can't do it with a class, because I get the value of the padding-top dynamically, here is the function :

$('.button').click(function(){
  $(this).toggleClass('active');
});

var tagsHeight = $('span').outerHeight();

$(".button").click(function (){
  if ($(this).is('active')) {
    $(".change").css('padding-top', '0');
  }
  else if ($(this).not('active')) {
    $(".change").css('padding-top', tagsHeight);
  }
});

An a example here : https://jsfiddle.net/o1pbwfuo/

I really don't get why this is not working correctly ...

Thanks !

4
  • 2
    This what you want ? jsfiddle.net/o1pbwfuo/3 Commented Jul 26, 2017 at 12:45
  • not a good idea, better toggle class, Commented Jul 26, 2017 at 12:45
  • @CarstenLøvboAndersen Løvbo Andersen thx mate :) Commented Jul 26, 2017 at 12:51
  • 1
    @ÁlvaroTouzón I can't because the value will change with the size of the viewport Commented Jul 26, 2017 at 12:52

4 Answers 4

5

The issue with your code is due to the incorrect selector in not(). That being said, you can improve your logic by combining the click() event handlers, then using a single ternary expression to set the padding-top on the required element based on the related class. Try this:

var tagsHeight = $('span').outerHeight();

$('.button').click(function() {
  var active = $(this).toggleClass('active').hasClass('active');
  $(".change").css('padding-top', !active ? '0' : tagsHeight);
});

Working example

Sign up to request clarification or add additional context in comments.

1 Comment

Nice and simple, as it should be.
1

You made a mistake while using .is and .not. You need to address the class itself inclusive the dot at beginning.

$('.button').click(function(){
  $(this).toggleClass('active');
});

var tagsHeight = $('span').outerHeight();

$(".button").click(function (){
  if ($(this).is('.active')) {
    $(".change").css('padding-top', '0');
  }
  else if ($(this).not('.active')) {
    $(".change").css('padding-top', tagsHeight);
  }
});

https://jsfiddle.net/06ek4fej/


By the way, the else-if request is nonsense. If = true or if = false. Else If results the same as else.

$(".button").click(function (){
  if ($(this).is('.active')) {
    $(".change").css('padding-top', '0');
  }
  else {
    $(".change").css('padding-top', tagsHeight);
  }
});

4 Comments

if ($(this).not('.active')) will always be truthy and is not a valid check....no need for the extra if() anyway
Why not use hasClass?
@charlietfl yes, was pointing on that while you posted the comment ;)
@Chaaampy Good. Best because shortest solution was delivered by Rory. Give him the vote ;)
1

On click you can check whether element has active class or not and there is no need to add two click methods on '.button'.

var tagsHeight = $('span').outerHeight();

$(".button").click(function (){
  $(this).toggleClass('active');
  if ($(this).hasClass('active')) {
    $(".change").css('padding-top', '0');
  }
  else{
    $(".change").css('padding-top', tagsHeight);
  }
});

Comments

1

You can use Has class method for the check is active class exist or not.

$(".button").click(function (){
  if ($(this).hasClass('active')) {
    $(".change").css('padding-top', '0');
  }
  else{
    $(".change").css('padding-top', tagsHeight);
  }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.