Using jQuery to toggle CSS properties

I'm having trouble switching between a CSS column layout and a normal layout for a div element with the ID 'article'. The jQuery script I wrote doesn't seem to work properly, as the entire thing disappears. Can anyone advise me on how to remove the CSS properties using jQuery in the toggle function? I've come across examples like the one below, but they don't seem to be effective. Can someone please guide me on the correct approach?

function changeview(orientation){
    $('#article').toggle(function(){
       $('#article').css('-webkit-column-width', '768px', 'height', '885px');                          
    },function(){                                 
        $('#article').css('-webkit-column-width', '', 'height', '');                              
    });
}

Answer №1

If you want to change the CSS of an element, you can easily do so using addClass and removeClass methods.

<style>
    .newCSS {
        -webkit-column-width : 768px;
        height : 885px;
    }
</style>

Simply use jQuery like this:

$('#article').toggleClass("newCSS");

I have created a simple demo on JSFiddle for you to check out: http://jsfiddle.net/VXmZp/

Answer №2

CHECK IT OUT LIVE

function adjustView(orientation){ 
    $('#article').click(function(){    
       var isTallEnough = $(this).height() >= 885 ;         
       $(this).css({
          '-webkit-column-width': isTallEnough ? 'auto' : 768,
                        'height': isTallEnough ? 'auto' : 885
       }); 
    });
}

Answer №3

Looks like I was beaten to the punch.. I always end up spending way too much time on my fiddles: http://jsfiddle.net/e18Rz/ Using the toggleClass method as suggested by user tymeJV is indeed a better approach.

Update - I have simplified the example to use only the toggleClass method - check it out here: http://jsfiddle.net/e18Rz/1/

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

An error occurred when attempting to run the command npm run compile:sass, displaying the message: npm ERR! missing script:

Everything seems to be in place with the sass folders and files, so what could be the issue? I have my package.json file set up correctly with the following code: { "name": "starter", "version": "1.0.0", " ...

Setting a date in Angular Material 2 without closing the mdMenu

I am trying to interact with Material 2's menu. Is there a way to select a menu item without closing the menu popup? Check out this link for more information. ...

Implementing AJAX for PHP error handling

I've built a PHP and AJAX powered page where user-submitted forms are validated in the getData.php script. In this scenario, if a user submits the form with the default value, I'm exploring ways to pass back errors or trigger an AJAX error respo ...

Is there a way to automatically update the input value when I refresh the page following a click event?

Currently, I have multiple p elements that trigger the redo function upon being clicked. When a p element is clicked, the quest[q] template is loaded onto the .form div. These templates are essentially previously submitted forms that sent values to an obj ...

Unveiling the mystery behind the inner workings of the (function(){

Hello everyone, I'm fairly new to programming and trying to wrap my head around a confusing method. var delay = (function(){ var timer = 0; return function(callback, ms){ clearTimeout (timer); timer = setTimeout(call ...

Automatically choose radio buttons within an li element in a loop

Hey there, I'm new to SO and this is my first question. As a bit of a newbie, I found this jquery code snippet on another SO answer that I want to use. The function I'm aiming for is the same, but the markup structure in my HTML is different bec ...

Determine if the "Enter" key has been pressed and validate the

How can I implement Enter key press functionality to login to the system using JavaScript? Below is the code snippet: @using (Html.BeginForm("Login", "InventoryBarcode", FormMethod.Post, new { id = "main" })) { <label for="LoginName" class="uname ...

JavaScript threw an error with message: 'Unexpected identifier' that was not caught

Upon launching Web Developer in Firefox: SyntaxError: missing } after property list note: { was opened at line 7, column 7 ...

What are the steps for implementing a equirectangular image in WebXR with three.js?

Currently, I am developing a web application with Three.js that requires the use of equirectangular images for panoramic tours. My goal is to incorporate a feature that allows users to switch between normal mode and VR mode similar to React360 and AFrame. ...

Unable to display images in the ngImgCrop upload preview modal screen

Currently, I am utilizing ngImgCrop to enable an upload preview and square crop feature for profile pictures. Unfortunately, I am experiencing issues where neither the image preview nor the cropping functionality are being displayed. 'use strict ...

Generatively generating a new element for the react application to be mounted on

I am looking to enhance my JavaScript application using React by creating a build. Currently, the application consists of just a single file structured as follows. This file essentially creates a div element and continuously changes the color of the text & ...

Unlocking hidden gridview column values with the power of jQuery

Within my gridview control, there are 4 columns, with one column being invisible which contains the Email Address information. <asp:GridView id='gridData' runat='server'> <Columns> <asp:TemplateField> ...

Explore button that gradually decreases max-height

I have a "Show More" button that expands a div by removing the css attribute max-height, but I want to add an animation similar to jQuery's slideToggle() function to smoothly reveal the rest of the content. This is the code I am using: <div id="P ...

prompting the JavaScript hangman game to identify the letters in the "selected word"

Currently, I am on a mission to teach myself Javascript and have taken on the challenge of creating a simple hangman game. This type of project is commonly used in interviews or tests, so it seemed like a great opportunity for practice. My approach involve ...

Locate all posts associated with the specified User ID

Using mongoose, I am able to populate the "Post Schema" with relevant information about users who create the posts. postModule.js const mongoose = require('mongoose'); const postSchema = mongoose.Schema({ title:String, description:String, date ...

Utilizing BEM Class Names in React

How can I utilize the Post component in a way that assigns unique classes to new and old posts following BEM recommendations? Assign a unique className to every element Avoid cascading dependencies like (.posts-new post or .posts-old post) Each component ...

Implementing a PHP button update functionality sans utilizing an HTML form

I need a way to update my database with just a click of a button, without using any forms or POST requests. Most examples I've seen involve updating through forms and the $_POST method. Is there a simpler way to update the database by directly click ...

What is the function type in React-Native?

I need assistance understanding the following function: const myFunction = () => () => { //perform a task } Can someone explain the meaning of this function? ...

ng-grid cell onclick callback

Currently, I am working with ng-grid and am attempting to define a callback function for when a cell is clicked. During this callback, it is crucial for me to identify the specific row and column of the cell that was clicked. Upon exploring various options ...

Ways to ensure certain code is executed every time a promise is resolved in Angular.js

Within my Angular.js application, I am executing an asynchronous operation. To ensure a smooth user experience, I cover the application with a modal div before initiating the operation. Once the operation is complete, regardless of its outcome, I need to r ...