Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

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

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #27383
    Filmguy15
    Member

    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!

    #68818
    kobylecki
    Member
    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)
  • The forum ‘JavaScript’ is closed to new topics and replies.