2

Is there way to get true when match both selectors class and data attribute ?

Find matched dropdown e.g:

`<select class="type-featured" data-featured="normal">`
....
`<select class="type-featured" data-featured="classical">`
....
`<select class="type-featured" data-featured="modern">`

Find select, class equal to type-featured AND data-featured value equal to normal - here is code:

$('.style-wrapper').each( function() {
    var $styles = $(this);
    if ( $styles.parents().find('select').is('.type-featured, [data-featured=normal]') ) {
    // do something here
    }
});

I got TRUE whether select has type-featured class OR data-featured="normal", data-featured="classical", data-featured="modern"

Its seems to be TRUE if match any one selector.

Is possible to get my desired result using .is function? May be using anonymous function like :

.is( function() {
//here some logic to result
}); 
0

2 Answers 2

7

If you deploy the jQuery:

$('select.type-featured[data-featured="normal"]').each(function(){

    // [... DO SOMETHING...]

});

you will run the function on only those <select> elements which have:

  • class="type-featured"
  • data-featured="normal"
Sign up to request clarification or add additional context in comments.

2 Comments

I write this example code. My original code is big and difficult to explain. May I not explain well. Okay, if we have select, input and textarea then how to match?
Using a comma, you can specify any number of selectors to combine into a single result. See: api.jquery.com/multiple-selector eg. $('select.type-featured[data-featured="normal"], input.type-featured[data-featured="normal"], textarea.type-featured[data-featured="normal"]')
0

Instead of is(...) try with hasClass() - this determine whether any of the matched elements are assigned the given class.

1 Comment

The .hasClass() method will return true if the class is assigned to an element. Here is class and data attribue

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.