0

I have a div with class of .ban2 and id of #banner2.

I want to change the border-radius property of this div whenever I click on the div itself. Like when iI click it for the first time the border-radius becomes 0 and when I click again it goes back to 50% 0 0 50%.

I want to toggle between these 2 styles. in my .ban2Toggle class I have all the styles included, but the border radius is 0.

it is only working once like when I click it for the first time the border radius becomes 0. The next time I click it I don't know how to get it back to normal.

.banner .ban2{
  position: absolute;
  right: 0;
  top: 0;
  width: 40%;
  height: 63vh;
  background: #5a0980;
  border-radius: 50% 0 0 50%;
  transform: scaleY(1.6);
  display: flex;
  justify-content: center;
  align-items: center;
  transition: 1s ease;
}

$('#banner2').on('click', function(){
  $('#banner2').removeClass('ban2');
  $('#banner2').addClass('ban2Toggle');
});

3 Answers 3

1

For jQuery, you must use

$(element).toggleClass('className');

It will automatically add and remove it for you.

For JavaScript, you must use

element.classList.toggle('className'); 

It will automatically add and remove it for you.

Example Snippet (Run)

$('#banner2').on('click', function() {
  $('#banner2').toggleClass('ban2Toggle');
});
.banner .ban2 {
  position: absolute;
  right: 0;
  top: 0;
  width: 40%;
  height: 63vh;
  background: #5a0980;
  border-radius: 50% 0 0 50%;
  transform: scaleY(1.6);
  display: flex;
  justify-content: center;
  align-items: center;
  transition: 1s ease;
}

.banner .ban2.ban2Toggle {
  border-radius: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="banner">
  <div id="banner2" class="ban2">Lorem Ipsum</div>
</div>

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

1 Comment

but i already have .ban2 class present. i want it to be removed and be replaced by .ban2Toggle
1

Following the answer from BOZ you need to do it like this:

html:

<div id="banner2" class="ban2"></div>

script:

$('#banner2').on('click', function(){
  $('#banner2').toggleClass('ban2');
  $('#banner2').toggleClass('ban2Toggle');
});

Comments

0

take a variable of naming flag=false; When you click on the button change the state of the flag variable to true When the flag is true make border radius 0 using conditional rendering When click again make flag false again When the flag is false make border radius 50 using again conditional rendering

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.