drake035
drake035

Reputation: 2877

Error with jQuery contains()

The following statement yields an error:

TypeError: $(...).children(...).contains is not a function

$('.woocommerce-checkout .shop_table tr.cart_item dl.variation').each(function() {
    if ($(this).children('.variation-Billing').contains("Once Off")) {
        $(this).children('.variation-Billing').contains("Once Off").show().siblings('.variation-Billing').show();
    }
}); 

What did I do wrong?

Upvotes: 1

Views: 913

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337560

The error is correct; 'contains' is not a method - it's a selector, use :contains().

Also note that the if statement should check the length property of the jQuery object to see if any elements were found. Try this:

$('.woocommerce-checkout .shop_table tr.cart_item dl.variation').each(function() {
    var $oneOffBillings = $(this).children('.variation-Billing:contains("Once Off")');
    if ($oneOffBillings.length) {
        $oneOffBillings.show().siblings('.variation-Billing').show();
    }
}); 

Upvotes: 4

Related Questions