0

I am trying to determine if an image source contains a string when I click on the image. The images have the class .swap-color. I have the variable $current_swatch set to the image src attribute, and it tests successfully. My code is below. No matter what image I click on, I get the alert "Contains TB", even if TB isn't in the image src. What am I doing wrong?

<img src="/images/Swatch-TB.jpg" class="swap-color"/>

$("document").ready(function () {
    $('.swap-color').click(function () {
        //get the image src
        var $current_swatch = $(this).attr('src');
        //check if TB is in the src
        if ($('$current_swatch:contains("TB")').length > 0 ) {
            alert ('Contains TB');
        } else {
            alert ('Does not contain TB');
        }
    });
});
5
  • Variables aren't expanded inside strings in Javascript, you should use concatenation. Commented Feb 11, 2016 at 2:45
  • And :contains isn't for searching strings, it's for searching DOM elements. Commented Feb 11, 2016 at 2:46
  • Why the downvotes? This is a perfectly reasonable, well-constructed question? Commented Feb 11, 2016 at 2:48
  • Some people downvote any question that indicates the poster is clueless and hasn't done any debugging on their own. Like use the JS console to see what $('$current_swatch:contains("TB")') returns. Commented Feb 11, 2016 at 2:50
  • @Barmar - Well, it's nice to bump into you again here on SO, and thank you for all your hard work! Good comments, good editing. Thanks for the insight! Commented Feb 11, 2016 at 2:52

2 Answers 2

4

There's enough feedback that I'm going to go out on a limb and post an answer, even though the key to your question could be done in a comment.

The way to check for the contents is to use the JS native indexOf(), rather than the jQuery selector method in your code.

Here's some commented revisions to your code:

// Streamlined, conflict-safe document ready
jQuery(function ($) {
    $('.swap-color').click(function() {
        //get the image src
        var $current_swatch = $(this).attr('src');
        //check if TB is in the src
        // Use JS native "indexOf" rather than jQuery 
        if ($current_swatch.indexOf('TB') > -1 ) {
            alert ('Contains TB');
        } else {
            alert ('Does not contain TB');
        }
    });
});

For more information on different ways to check for a substring, check out this answer: Fastest way to check a string contain another substring in Javascript?

Sign up to request clarification or add additional context in comments.

Comments

1

So, you're using the jQuery's contains method which checks if a selector contains a value. The problem is, $current_swatch is actually a string, since $(this).attr('src') will give "/images/Swatch-TB.jpg".

Instead what you want is the vanilla JS includes.

if ($current_swatch.includes('TB')) {
  ...

11 Comments

Isn't includes for arrays? Not strings per-se?
@cale_b not at all. Array and String both have includes and indexOf as part of their prototypes. Look at the doc I linked to - it's for String. I would argue in OP's case includes is more clear since OP isn't looking for the index, just if the value exists.
@cale_b to go further, if you really think about it, strings are implemented as arrays, so it makes sense that they would have many of the same methods.
Yes, agreed on strings == arrays. And nice to learn about includes - always learning on SO. Thanks!
@MatthewHerbst Not really, since the array methods look for individual elements, while the string methods look for substrings.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.