dennismonsewicz
dennismonsewicz

Reputation: 25542

jQuery search ID for certain text

I am looking for a way to search for an ID on the current clicked element.

Example:

$('.t_element').click(function(){
            if(!$('.t_element [id*=meta]')) {
                transModal($(this));
                translateText();
            }
        });

I basically need to say if the clicked element id does not contain the word "meta" then continue with the script, but this is not working.

Sorry if this is confusing.

Thanks! Dennis

Working Example:

if (!$(this).is('[id*=meta]')) {
    transModal($(this));
    translateText();
}

Upvotes: 0

Views: 280

Answers (3)

lonesomeday
lonesomeday

Reputation: 237975

Sarfraz's version should work. Another method is using .is():

if ($(this).is('[id*=meta]')) {
    transModal($(this));
    translateText();
}

Or, as Patrick says, if you want not to act if the element has an id containing "meta":

if ($(this).not('[id*=meta]')) {
    transModal($(this));
    translateText();
}

Upvotes: 1

user113716
user113716

Reputation: 322542

If the IDs of the elements aren't going to change, then I'd just include the test in the selector so you're not assigning unneeded .click() handlers.

$('.t_element:not([id*=meta])').click(function(){
    transModal($(this));
    translateText();
});

This uses the :not() selector along with the attribute contains selector to prevent the handler from being assigned to elements where the ID contains meta.

Upvotes: 2

Sarfraz
Sarfraz

Reputation: 382806

Try with length like this:

$('.t_element').click(function(){
   if($('[id*="meta"]', $(this)).length === 0) {
     transModal($(this));
     translateText();
   }
});

Or:

$('.t_element').click(function(){
   if($(this).attr('id').indexOf('meta') <= -1) {
     transModal($(this));
     translateText();
   }
});

Upvotes: 1

Related Questions