Click to Rotate the Shape

I am attempting to apply a rotation effect on a shape when clicked using jQuery and CSS. My goal is to select the element with the id of x and toggle the class rotate on and off. The rotate class utilizes the CSS transform property to achieve the desired rotation.

$('#x').click(function() {
  $('#x').toggleClass('.rotate');
});
.container {
  height: 500px;
  width: 960px;
  margin: 0 auto;
  display: flex;
  background: skyblue;
}

#x {
  margin: auto;
  font-size: 9em;
  color: green;
}

.rotate {
  transform: rotate(7deg);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div id="x">x</div>
</div>

Answer №1

When using certain functions in jQuery like addClass, removeClass, toggleClass, there is no need to include a dot before the class name since it is already implied that it is a class. For example:

$(this).removeClass("yourClass");
$(this).addClass("yourClass");
$(this).toggleClass("yourClass");

Your corrected question:

$('#x').click(function(){
    $('#x').toggleClass('rotate');  
});
 .container{
      height: 500px;
     width: 960px;
     margin: 0 auto;
     display: flex;
     background: skyblue;
     }
    #x{
    margin: auto;
    font-size: 9em;
    color: green;
    }
    .rotate{
    transform: rotate(7deg);
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<div class="container">
  <div id="x">x</div>
  </div>

Answer №2

There seems to be a mistake in your Javascript code:

$('#x').click(function() {
  $('#x').toggleClass('.rotate');
});

It looks like you are using the toggleClass function, so there is no need to include a . before your selector. Instead of:

toggleClass('.rotate')

You should use:

toggleClass('rotate')

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

minimize javascript syntax (stackoverflow 2022)

I'm struggling with this puzzle game. Sometimes, when I start a new game, the pieces get shuffled incorrectly. I tried adding a function to fix it, but it doesn't seem to be working. It's really frustrating and I want to simplify the code as ...

What's the best way to display the mysqli_error message when making an AJAX call with jQuery?

How can I display the mysqli error message during an ajax call? Here is my PHP code: if(isset($_POST['resumetitle']) || isset($_POST['name']) || isset($_POST['dob']) || isset($_POST['gender']) || isset($_POST[&apos ...

Issues related to the performance of React, Redux, and three.js

UPDATE: After identifying the issue, I have narrowed it down to this small gist and this jsfiddle. In my real-time 3D game based on three.js, I intended to utilize Redux for state management. Despite creating a simple prototype for testing purposes, even ...

Tips for implementing multiple nested routes using Vue.js?

Can Nested Routes be created with more than 2 levels? I am looking to implement a structure like this: +--------------------+ | User | | +----------------+ | | | Profile | | | | +------------+ | | | | | About | | | | | | +------ ...

How to use a self-hosted font in a custom Material-UI theme for ReactJS?

Currently, I am in the process of setting up my initial project utilizing Material-UI for ReactJS. My intention is to personalize the default theme by incorporating a custom font (from the server, not sourced from Google Fonts or anything similar). Despite ...

Adjust the height for just one md-tab

Looking to find a way to set the height of the content within an <md-tab> element to be 100% in a flexible manner. For example, consider the following structure: <body> <md-content> <md-tabs> <md-tab label= ...

Is it possible to dynamically alter the text and contrast of your entire website?

In the realm of MVC 4 and Visual Studio 2013: I am pondering over how to dynamically change the text on my website with the simple click of a button. The same goes for adjusting contrast. Are there any plugins or techniques that could facilitate this proc ...

Running a series of functions consecutively with JQUERY

Currently, I am facing an issue with using an ajax method .load to replace the content of a div. The library I am working with does not allow me to replace the content of a div as some functions continue to run in the background. To overcome this challeng ...

Creating a User-friendly Layout for Admin Pages in Next.js Version 13

Hey there, I'm facing an issue with the layout while using Next.js 13 Experimental App Directory. On my website's index page or routes '/', I want to show a landing page and use a specific layout for all pages except for those under the ...

Error: An unexpected symbol '<' was encountered after the build process in Vue.js

I just finished deploying a MEVN stack application to heroku. While everything is functioning properly locally, I am encountering a blank page and the following errors in the console post-deployment: Uncaught SyntaxError: Unexpected token '<' ...

The best approach to incorporating interactive animation in next.js

My vision is to develop a character creation application using next js. The app should empower users to customize the character using sliders and gender selection buttons. The ultimate goal is to have a 2D animated version of the character that dynamicall ...

The annoying Facebook "add a comment" popup refuses to close

Occasionally, the "add a comment" popup (iframe) in Facebook's Like plug-in fails to close and obstructs access to the content underneath. This issue has been noted while using Chrome 21 and Firefox 15. To replicate this problem, you can visit the fo ...

Attempting to utilize a Jquery Ajax post method to retrieve information from my specified URL and display it within my HTML Div element

Seeking assistance in fetching real-time data using a jQuery Ajax Post method. My experience lies mainly in SQL and C#, so coding in this language is relatively new to me. I was advised to use this script, which seemed straightforward. However, despite my ...

Chrome Extension: Passing a variable between Content Script and background.html

Struggling to understand how to transfer a variable or an array of variables from a content script to a background page. The goal is to identify specific DOM elements using the content script and then send them to the background page for a cross-domain XML ...

The Jquery.post() function failed to trigger the callback

I'm encountering an issue with the JQUERY Post function. There are 2 functions that utilize the JQUERY Post function. Both of them work fine, but the callback function (handleLike) is not being triggered. Interestingly, when I manually call handleLi ...

In my chat application, I encountered the error message "Received an expression instead of an assignment or function call - no-unused-expressions"

While working on my React Chat app and trying to access my Firebase, I encountered the error message: "Expected an assignment or function call and instead saw an expression no-unused-expressions" The error seems to be related to the assignment of this.rem ...

Execute a PHP function using JavaScript/jQuery with AJAX requests

Hello everyone, I am reaching out for assistance with AJAX as I am still quite new to it. Despite the abundance of examples available, I find it challenging to grasp the concept. Specifically, I need help with my validation of a contact form using JavaScri ...

Unable to upload gathered email to Mailchimp mailing list through Nodejs and express API

Seeking guidance on integrating data collection with Mailchimp in a Nodejs backend project. I am currently working on an email signup list page where users input their first name, last name, and email. The HTML file contains fields named firstName, lastN ...

What is Mozilla's reasoning for deeming Conditional catch-blocks as non-conventional?

I recently came across a document on Mozilla that described the try...catch conditional as "non-standard and is not on a standards track. Do not use it in production" However, I am curious to understand the reason behind this statement. I also want t ...

Display/Conceal a div using CSS in React JS/Redux

I've been working on a CSS toggle for a div with className toggling. My approach involves using redux to manage the boolean state. Using the redux chrome extension, I can see that the state is indeed changing but the div remains invisible. If you have ...