views:

41

answers:

3

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();
}
+2  A: 

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();
   }
});
Sarfraz
@Sarfraz Fixed a typo for you.
lonesomeday
@lonesomeday: Oh I am sorry I edited it again :(
Sarfraz
@Sarfraz all your comments for my question are gone ,why so any idea
gov
@gov: They are there :)
Sarfraz
+1  A: 

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();
}
lonesomeday
This works exactly how I want it to! Thanks!!!
dennismonsewicz
@dennismonsewicz - In your question you state that you want the code to run only if `meta` is *not* in the ID. So which do you actually want?
patrick dw
Whoops, yes. Thanks, patrick.
lonesomeday
@patrick, I need it to work if "meta" is not in the ID. I put a ! in front of the statement and its working. I put my working example above
dennismonsewicz
+2  A: 

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.

patrick dw