javascript - How to use jQuery :not selector for more than one element at a time -
i want use jquery :not selector in such way when bunch of elements don't have specified class, want add class 1 of them.
what want achieve is: 'when design-preview1, design-preview2 design-preview 3 , design-preview 4 not have class "selected", add class "selected" "design-preview1" '.
i tried not working:
$(".design-preview1, .design-preview2, .design-preview3, .design-preview4").not(".selected").$(".design-preview1").addclass('selected'); });
any highly appreciated
new
this full jquery code:
$('.pop-design1').click(function(){ $(".design-preview li:not(.design-preview2).selected").removeclass('selected'); $(".design-list li:not(.design-list2).selected").removeclass('selected'); $(".design-preview2").toggleclass('selected'); $(".design-list2").toggleclass('selected'); $(".overlay").toggle(); }); $('.pop-design2').click(function(){ $(".design-preview li:not(.design-preview3).selected").removeclass('selected'); $(".design-list li:not(.design-list3).selected").removeclass('selected'); $(".design-preview3").toggleclass('selected'); $(".design-list3").toggleclass('selected'); $(".overlay").toggle(); }); $('.pop-design3').click(function(){ $(".design-preview li:not(.design-preview4).selected").removeclass('selected'); $(".design-list li:not(.design-list4).selected").removeclass('selected'); $(".design-preview4").toggleclass('selected'); $(".design-list4").toggleclass('selected'); $(".overlay").toggle(); }); if ($('.design-preview1, .design-preview2, .design-preview3, .design-preview4').not('.selected').length) { $('.design-preview1').addclass('selected');
want when .design-preview1, .design-preview2, .design-preview3 , .design-preview4 not showing or not selected, want .design-preview1 show default.
live link: http://www.expertfrontend.com/test/2.html
what want achieve is: when design-preview1, design-preview2 design-preview 3 , design-preview 4 not have class "selected", add class "selected" "design-preview1"
as requested in question, said want use .not()
achieve this. below way can via addselectedclassifnotexists()
function.
you can refactor click listeners, run 1 defined function. below modified code question after suggested changes:
var previews = $('.design-preview1, .design-preview2, .design-preview3, .design-preview4'); function addselectedclassifnotexists() { if (previews.not('.selected').length === previews.length) { $('.design-preview1').addclass('selected'); } } function toggleclasses(num) { $('.design-preview li:not(.design-preview' + num + ').selected').removeclass('selected'); $('.design-list li:not(.design-list' + num + ').selected').removeclass('selected'); $('.design-preview' + num).toggleclass('selected'); $('.design-list' + num).toggleclass('selected'); $('.overlay').toggle(); } previews.click(function () { toggleclasses(this.classname.slice(-1)); addselectedclassifnotexists(); });
Comments
Post a Comment