Grow your CSS skills. Land your dream job.

If element has class that contains "word", do something…

  • # December 30, 2009 at 2:06 pm

    Hello,

    This is what I’m trying to achieve:

    When a button is clicked, have jQuery look at it’s class. If it’s class has the word "print" in it, or even starts with "p", do some special animation. If it has a class that contains "video", then do a different animation.

    Here is my code so far:

    Code:
    $(“#grid li a”).click(function() {
    var buttonClass = $(this).attr(‘class’);
    if (buttonClass == ‘print’) {
    // DO SOMETHING
    }
    });

    The problem is, that simply saying "==" won’t work, since the button classes in my html are "print01", "print02", etc… which is used for other purposes like knowing which print div to show. So how can I get it to just look for the word "print" instead of having to match the full class name exactly? Thanks!

    # December 31, 2009 at 10:47 am
    Code:
    var buttonClass = $(this).attr(‘class’);
    if (print.test(buttonClass)) {
    // DO SOMETHING
    }

    print is a regular expression, see this: http://w3schools.com/js/js_obj_regexp.asp

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".