2

I am trying to toggle between css column layout and a normal layout of a div element with id 'article', but the jQuery is not working... the whole thing just disappears. can anyone tell me how to remove the css properties with jQuery in the toggle function ? I read that you can remove properties like have it below... but it is not working... can anyone please tell me the right way to do it ?

function changeview(orientation){
    $('#article').toggle(function(){
       $('#article').css('-webkit-column-width' : '768px', 'height' : '885px');                          
    },function(){                                 
        $('#article').css('-webkit-column-width' : '', 'height' : '');                              
    });
}
6
  • don't set CSS properties to empty strings ''. If you want to set height to the default, set it to auto. Same with -webkit-column-width, although you can use just column-width for that and it will work in modern browsers. Commented Mar 13, 2013 at 19:10
  • 2
    you'd be better off adding/removing classes. Also your syntax is incorrect. Use $('#article').css({'-webkit-column-width':'768px'}); or $('#article').css('-webkit-column-width', '768px'); Commented Mar 13, 2013 at 19:12
  • Also, for the record, toggle is deprecated. Commented Mar 13, 2013 at 19:17
  • @MattDiamant what should I use instead of toggle then ?. I tried it with the auto but not working. Commented Mar 13, 2013 at 19:21
  • @Derek sorry that I forgot about the {}. Commented Mar 13, 2013 at 19:21

3 Answers 3

1

You can use the addClass and removeClass for this.

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

and the jQuery

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

Also made a fiddle (simple demo): http://jsfiddle.net/VXmZp/

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

1 Comment

what about only .toggleClass('newCSS'); ?
1

LIVE DEMO

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

1 Comment

Wrap the contents of .css in an object {} :-)
0

beat me to it.. always spend too long with my fiddles : http://jsfiddle.net/f64Wy/ toggleClass as suggested by tymeJV is even better.

update - same example stripped down to use just toggleClass - http://jsfiddle.net/f64Wy/1/

1 Comment

thanks for your answers... I will try it with the toggleClass

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.