Skip to main content

jQuery removeClass only working with actual class name, not var



I created a page to play around with the 960 Grid System, learn about div placement. There are up to four divs on the page. When a new grid size, pull or push value is selected from a dropdown, I need to remove the current class, i.e. grid_8 , pull_5 or push_4 , and add the newly selected class.







function alter_grid1(new_class_name){



var old = document.getElementById('grid1');

var classes = old.className.split(' ');

var old_class_name = 'cessna';

$.each(classes, function(){



if(old_class_name == 'cessna' || old_class_name == null){

old_class_name = this.match('/grid_1|grid_2|grid_3|grid_4|grid_5|grid_6|grid_7|grid_8|grid_9|grid_10|grid_11|grid_12|grid_13|grid_14|grid_15|grid_16|grid_17/');

}

});

$(old).removeClass('grid_1 grid_2 grid_3 grid_4 grid_5 grid_6 grid_7 grid_8 grid_9 grid_10 grid_11 grid_12 grid_13 grid_14 grid_15 grid_16').addClass(new_class_name);

console.log('Old grid1 class name is ' + old_class_name + ' and New grid1 class name is ' + new_class_name);

}







I can use addClass(new_class_name); without any trouble.





I can use use removeClass('grid_8'); without any trouble (grid_8 being the actual class name)





When I use removeClass(old_class_name); it just doesn't work. No error message, nothing in firebug, just doesn't work.





Since I don't know what the previous value will be, I have to remove any possible class that could be there







$(old).removeClass('grid_1 grid_2 grid_3 grid_4 grid_5 grid_6 grid_7 grid_8 grid_9 grid_10 grid_11 grid_12 grid_13 grid_14 grid_15 grid_16').addClass(new_class_name);







That's the only way I can get it to work. Sure it works, but seems like I'm either missing something or doing something wrong. You can see what I'm trying to do here if it would help. I just don't see how I can add a class using a var, but not remove one the same way.


Comments

  1. Seems your regex is wrong. Use either a string without the slashes or just the slashes.

    old_class_name = this.match(/grid_1|grid_2|grid_3|grid_4|grid_5|grid_6|grid_7|grid_8|grid_9|grid_10|grid_11|grid_12|grid_13|grid_14|grid_15|grid_16|grid_17/);


    or maybe even

    old_class_name = this.match(/grid_[0-9]+/);

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex