Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use jQuery :not selector for more than one element at a time

I want to use the jquery :not selector in such a way that when a bunch of elements don't have a specified class, then i want to add a class to one of them.

What I want to achieve is: 'when design-preview1, design-preview2 design-preview 3 and design-preview 4 do not have the class "selected", add class "selected" to "design-preview1" '.

I tried this but it is not working:

   $(".design-preview1, .design-preview2, .design-preview3, .design-preview4").not(".selected").$(".design-preview1").addClass('selected');
    });

Any help would be highly appreciated

New

This is my 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');


Basically what I want is that when .design-preview1, .design-preview2, .design-preview3 and .design-preview4 are not showing or are not selected, I want .design-preview1 to show by default.
Live link: http://www.expertfrontend.com/test/2.html

like image 405
Alex Zahir Avatar asked Apr 26 '15 04:04

Alex Zahir


People also ask

How do you target multiple elements in jQuery?

To select multiple elements of an html page using multiple elements selector, we pass the element names inside parenthesis, in double quotes, separated by commas. For example: $(“div, p, h2”) this will select all the div, p and h2 elements of a page.

Can we use multiple selectors in jQuery?

You can specify any number of selectors to combine into a single result. This multiple expression combinator is an efficient way to select disparate elements. The order of the DOM elements in the returned jQuery object may not be identical, as they will be in document order.

What is $() in jQuery?

The jQuery syntax is tailor-made for selecting HTML elements and performing some action on the element(s). Basic syntax is: $(selector).action() A $ sign to define/access jQuery. A (selector) to "query (or find)" HTML elements. A jQuery action() to be performed on the element(s)


2 Answers

:not is a little too simple for what you are trying to do, as you need to change the class of multiple elements, and not all the same way. So, you're going to need a few steps. Among other things, you need to use the .filter method to determine whether any of your tags have the selected class:

var group = ".design-preview1, .design-preview2, .design-preview3, .design-preview4",
    $group = $(group);

function doDefaultSelection() {
    if ($(".design-preview1, .design-preview2, .design-preview3, .design-preview4")
        .filter(".selected").length === 0) { // note use of .filter here
        $(".design-preview1").addClass('selected');
    }
}

doDefaultSelection();

$group.click(
    function() {
        $group.not(this).removeClass('selected');
        $(this).toggleClass('selected');
        doDefaultSelection();
    }
);

The default selector function (doDefaultSelection) selects all elements with your designated classes, picks only ones that have the selected class, and checks to see if there are any left. If there are no matching elements, then it adds the selected class to the .design-preview1 element(s).

The click handler turns off the selected class for everything except for the clicked element, then toggles the class for the one you clicked on. It then sets the default again if necessary.

Demo

var group = ".design-preview1, .design-preview2, .design-preview3, .design-preview4",
    $group = $(group);

function doDefaultSelection() {
    if ($(".design-preview1, .design-preview2, .design-preview3, .design-preview4")
        .filter(".selected").length === 0) {
        $(".design-preview1").addClass('selected');
    }
}

doDefaultSelection();

$group.click(
    function() {
        $group.not(this).removeClass('selected');
        $(this).toggleClass('selected');
        doDefaultSelection();
    }
);
.selected {
    color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p class="design-preview1">blah</p>
<p class="design-preview2">blah</p>
<p class="design-preview3">blah</p>
<p class="design-preview4">blah</p>
like image 197
elixenide Avatar answered Oct 08 '22 21:10

elixenide


What I want to achieve is: when design-preview1, design-preview2 design-preview 3 and design-preview 4 do not have the class "selected", add class "selected" to "design-preview1"

As requested in your question, you said you want to use .not() to achieve this. Below is a way you can do this via the addSelectedClassIfNotExists() function.

You can also refactor your click listeners, so you only run one defined function. Below is what your modified code from your question could look like after the 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();
});
like image 34
boombox Avatar answered Oct 08 '22 22:10

boombox