0

I'm trying to use jQuery to add classes to a table for a Wordpress site. I've had luck using nth-child(number) for selecting a specific td containing specific info. However, some cells/td's are containing several different information, so I want to select and target based on what they contain. Specifically, I want to select a td based on that it starts with or contains a number, but I can't figure out a way to do this. I've tried using different if statements, but can't get it to work.

Here's the code I use for the other cells, which contains the same information throughout the table:

$( "td:nth-child(5)" ).not( "td:empty" ).addClass("wine-currency");

Any help would be appreciated.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • There's nothing built-in that does this, you'll have to use `.filter()` and test the contents. – Barmar Mar 23 '18 at 15:04
  • Care to elaborate on how to or point me in a direction where I can figure it out? I'm still really green in jquery. – Peter Jensen Mar 23 '18 at 15:08
  • This may help, you could give a try to [the includes() method](https://www.w3schools.com/jsref/jsref_includes.asp) – DIEGO CARRASCAL Mar 23 '18 at 15:09
  • I told you the exact function to use. I don't see how I can point you more in the direction without just doing it for you entirely. – Barmar Mar 23 '18 at 15:10
  • In the filter function, use `.text()` to get the text of the TD, and use a regular expression to test if it contains a number. – Barmar Mar 23 '18 at 15:11
  • 1
    @DIEGOCARRASCAL `includes()` needs a specific string to search for, he wants to match any number. This is a job for a regular expression. – Barmar Mar 23 '18 at 15:11
  • [Then this is his answer](https://stackoverflow.com/questions/9011524/javascript-regexp-number-only-check?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) – DIEGO CARRASCAL Mar 23 '18 at 15:42
  • Found a way, thanks for helping out! – Peter Jensen Apr 18 '18 at 12:40

0 Answers0