Charles Marsh
Charles Marsh

Reputation: 465

jQuery contains not working

Is there something wrong with the code below it just won't work, no errors?

var select_value = $("#cart-image").attr('alt');

if ($("select_value:contains('Aqua')")) { keyword = "aqua"; };

Upvotes: 0

Views: 978

Answers (4)

David Tang
David Tang

Reputation: 93664

While all the other answers are correct, they forget to mention that you can use jQuery for this. There is the Attribute contains selector:

if ($("#cart-image[alt*=Aqua]").length) keyword = "aqua";

Upvotes: 4

Bobby Jack
Bobby Jack

Reputation: 16018

.attr() returns a string, not a DOM element

Upvotes: 0

Will
Will

Reputation: 6286

The double quotes are making jQuery interpret the "select_value" as a string, rather than the variable.

try

if (select_value.indexOf('Aqua') != -1) { keyword = "aqua"; };

Upvotes: 1

lonesomeday
lonesomeday

Reputation: 237827

You're using jQuery to do something that it is neither designed for nor capable of. Use Javascript's native functions to search strings for substrings:

if (select_value.indexOf('Aqua') > -1) {
    keyword = 'aqua';
}

Upvotes: 1

Related Questions