0

Hy guys, I would like to know how remove css styles after click event,

In fact at the first click I add css and remove some other things but I would like at the second click get the same css code at the beginning.

$('#show').click(function (){
  $('.sub-menu').css('width', '185px');
  $('.sub-menu').css('-webkit-transform', 'none');
  $('.sub-menu').css('transform', 'none');

  // and second click 

  $('.sub-menu').css('width', '');
  $('.sub-menu').css('-webkit-transform', 'scale(0)');
  $('.sub-menu').css('transform', 'scale(0)');

})

I don't want use toggleclass method cuz I don't want touch the css file.

Thanks.

2 Answers 2

4

A very simple option is to keep some kind of boolean marker.

var applied = false;

$('#show').click(function() {

  if (!applied) {
  
    $('.sub-menu').css('width', '185px');
    $('.sub-menu').css('-webkit-transform', 'none');
    $('.sub-menu').css('transform', 'none');
    applied = true;

  } else { // and second click 

    $('.sub-menu').css('width', '');
    $('.sub-menu').css('-webkit-transform', 'scale(0)');
    $('.sub-menu').css('transform', 'scale(0)');
    applied = false;
    
  }

})

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

Comments

0

You mentioned that you don't want to touch the css file. How about you create a css rule using jQuery and append it to the head tag.

var rule = $('<style>.myclass{width:185px; -webkit-transform:none; transform:none;}</style>');

$('html > head').append(rule);

$('#show').on('click',function(){
    $('.sub-menu').toggleClass('myclass');
});

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.