JQuery: find element whose class contains value of variable

eve

I searched high and low now but I can't figure out how to get this to work:

I need to get the element whose class name contains a number that I pass with a variable.

For better understanding: This is inside a "click" event on a gallery. Whenever an img is clicked I search for the src, see which number the string contains and then want to find the matching p whose class contains the same number. So I can manipulate the matching text to the picture that is currently displayed (Since the plugin I use strips away any id or class I can only identifiy a picture by its source name).

So far it does work if I directly put the number in as a String. Like this:

var team_t = $(this).find("img").attr("src");

    for(n = 1; n <=6; n++ ){

            if(team_t.indexOf(n) != -1)
            {   
                $('#text_content').find("p[class*='3']").css("background-color", "red");

            }
    }

But instead of "3" I want it to get the number that the variable n holds. I tried this but it did not work:

$('#text_content').find("p[class*=' + n + ']").css("background-color", "red");

Actually it didn't work with any kind of variable I tried to pass. I also saw and tried examples that use contains( ) or hasClass( ) and others.. but nothing worked for me. I'm not extremly familiar with the syntax though.

How do I have to write this so it takes the variabel as a string?

If I do alert( n ) it shows the correct number, so that's not the problem.

T.J. Crowder

You were on the right track with string concatenation, you just didn't get all the way outside the string:

$('#text_content').find("p[class*='" + n + "']").css("background-color", "red");
// Change is here -----------------^- and -^

In your attempt, the + n + was still in the outer quotes, and so was used literally in the selector (making it fail).


That said, if you have any control over the DOM structure (but it sounds like you may not), there's probably a better way. For instance, you could put a data-* attribute on the element that gives an appropriate selector for the p element (possibly even by id, but that's just one option).


You might also want to end the loop once you've found the index, by putting break; on the line after the line setting the red color.


Finally: You might want to use a class to add the red color rather than mixing your presentation into your JavaScript code.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Find column whose name contains a specific string

From Dev

XPath to find a node whose first child node contains a certain value

From Dev

jquery find element with class and style display:block

From Dev

jQuery class selector vs cached element + find

From Dev

Xpath: Find an element whose descendant has got a certain attribute value

From Dev

jQuery Find class and store it as a Variable

From Dev

Reference to a bash variable whose name contains dot

From Dev

jquery find a selector whose id contains a string using regex

From Dev

Use goquery to find a class whose value contains whitespace

From Dev

Find element by class and custom attribute value (then update it)

From Dev

jQuery - if element = (class) then variable = (variable)

From Dev

How to find if an array element contains a specific value in JavaScript/jQuery?

From Dev

Python - find element if the previous contains specific type value in xpath

From Dev

Find and replace an element's class using jQuery

From Dev

jQuery - Find href that contains a value and return the full value

From Dev

How to find HTML element with a data attribute which contains a specific value?

From Dev

jQuery: Find next Element with same class

From Dev

jQuery find element in variable

From Dev

Xpath: select div that contains class AND whose specific child element contains text

From Dev

Jquery find/Contains and replace table cell value

From Dev

jquery find first element by class without id

From Dev

jquery find next element with class name

From Dev

find elements by class whose id contains a substring and change the style

From Dev

Expand a variable whose value contains an option and a pathname with whitespaces

From Dev

Javascript variable value in a class of an element?

From Dev

Find current value with class jquery

From Dev

Find element in List<> that contains two value

From Dev

Fastest Way to Find the Closest Element to Another Element whose Value is Less in R

From Dev

how to get a json variable Whose value contains a "-"

Related Related

  1. 1

    Find column whose name contains a specific string

  2. 2

    XPath to find a node whose first child node contains a certain value

  3. 3

    jquery find element with class and style display:block

  4. 4

    jQuery class selector vs cached element + find

  5. 5

    Xpath: Find an element whose descendant has got a certain attribute value

  6. 6

    jQuery Find class and store it as a Variable

  7. 7

    Reference to a bash variable whose name contains dot

  8. 8

    jquery find a selector whose id contains a string using regex

  9. 9

    Use goquery to find a class whose value contains whitespace

  10. 10

    Find element by class and custom attribute value (then update it)

  11. 11

    jQuery - if element = (class) then variable = (variable)

  12. 12

    How to find if an array element contains a specific value in JavaScript/jQuery?

  13. 13

    Python - find element if the previous contains specific type value in xpath

  14. 14

    Find and replace an element's class using jQuery

  15. 15

    jQuery - Find href that contains a value and return the full value

  16. 16

    How to find HTML element with a data attribute which contains a specific value?

  17. 17

    jQuery: Find next Element with same class

  18. 18

    jQuery find element in variable

  19. 19

    Xpath: select div that contains class AND whose specific child element contains text

  20. 20

    Jquery find/Contains and replace table cell value

  21. 21

    jquery find first element by class without id

  22. 22

    jquery find next element with class name

  23. 23

    find elements by class whose id contains a substring and change the style

  24. 24

    Expand a variable whose value contains an option and a pathname with whitespaces

  25. 25

    Javascript variable value in a class of an element?

  26. 26

    Find current value with class jquery

  27. 27

    Find element in List<> that contains two value

  28. 28

    Fastest Way to Find the Closest Element to Another Element whose Value is Less in R

  29. 29

    how to get a json variable Whose value contains a "-"

HotTag

Archive