0

I have a table with several cells with the same content. I would like just to select the first one and do some CSS changes.

The cells are not in the same row (tr)

What I have so far is:

jQuery('td:contains("Februar")').css('font-weight', 'bold');

That works for all the td's with 'Februar'. I tried to select just the first one with :first, but that doesn't seem to work.

Cytracon
  • 13
  • 4
  • 1
    Please include your attempt with `:first` - if you did `td:first:contains...` then it likely won't work. Try also `$("td:contains(...)").first().css...` [first()](https://api.jquery.com/first/) – freedomn-m Apr 01 '22 at 17:16

1 Answers1

0

All of the lines below will work.

jQuery('td:contains("Februar")')[0].style.fontWeight = 'Bold'; or jQuery('td:contains("Februar")').first().css('font-weight', 'bold'); or jQuery('td:contains("Februar")').eq(0).css('font-weight', 'Bold'); or jQuery('td:contains("Februar"):first').css('font-weight', 'bold');

Jeffhowcodes
  • 406
  • 3
  • 9
  • Thanks, but still all the 'Februar' will be bold with your code? I forgot to mention that the td's are in different rows 'tr', is that the reason? – Cytracon Apr 02 '22 at 03:40
  • I know this question was closed by another user. But if you can share your html on something like codepen and post a link, I can help. Otherwise I’m guessing at the structure of your html that you’re having trouble selecting with jQuery. – Jeffhowcodes Apr 02 '22 at 11:27
  • Thanks for your help, appreciate it. You can find the table here: https://schlussgang2.cytracon1.info/resultate/ – Cytracon Apr 02 '22 at 11:29
  • on line 1061 (at least that's the line in my dev tools, your file may have php in it), you have this line of code: `jQuery('td:contains("Februar"):first-child').css('font-weight', 'bold');` The :first-child pseudo-selector is what is causing all the feb's to be bold. Each one of those elements are the first-child within the tr. The code snippets I've included in my answer do not use this pseudo-selector. They all use a different method to select the first element in the document that fits the criteria. – Jeffhowcodes Apr 02 '22 at 12:06
  • Sorry, that was a test I forgot to take out. But unfortunately still not working. Thanks for the help! – Cytracon Apr 02 '22 at 12:37
  • Looks like your script is running too early. It works if I run it in the console after the page has loaded (and the table created). Try moving this script to the last line before the body tag closes. – Jeffhowcodes Apr 02 '22 at 12:51