0

I have table cells custom ids with their own value.

Can I find the one that has a known value with jQuery ?

In my example below I want to check if td with customId="three" equals three.

<table>
   <tr>
      <td customId="one">blah<td>
   </tr>
   <tr>
      <td customId="two">blah blah<td>
   </tr>
   <tr>
      <td customId="three">blah blah blah<td>
   </tr>
   <tr>
      <td customId="four">blah blah blah blah<td>
   </tr>
</table>
  • Did you try anything? It's hard to know which part you're having difficulty with. No need for jQuery. Look into https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector and [use a selector](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors) like `td#three`. – Ruan Mendes Feb 04 '22 at 13:30
  • 2
    @JuanMendes While I agree with the "jquery no need" policy, the OP asks for jquery for a reason of its own. At most if I had made an answer I would have given an alternative :) – Simone Rossaini Feb 04 '22 at 13:36
  • @SimoneRossaini It's not like I downvoted an answer because it used jQuery. I'm just saying you don't need jQuery for such simple tasks. Which one reads more naturally: `if ($("td#three").length)` or `if (document.querySelector("td#three")` ? – Ruan Mendes Feb 04 '22 at 14:01
  • @JuanMendes Actually in my opinion jQuery is never needed, it is not a question of how natural it is or not, simply jQuery is suitable for those who do not want to fully learn the language thus making the front-end part easier – Simone Rossaini Feb 04 '22 at 14:12
  • There are times when jQuery will be very helpful and VanillaJs will take a lot more code. I can think of [event delegation](https://learn.jquery.com/events/event-delegation/), [animations](https://learn.jquery.com/effects/intro-to-effects/) and actions on sets of elements, such as `$( "h1" ).css( "fontSize", "100px" )` – Ruan Mendes Feb 04 '22 at 14:42
  • @JuanMendes which reads more naturally? the first. But then this is a [tag:jquery] question, tagged [tag:jquery] and not tagged javascript (if it was, then fine). – freedomn-m Feb 04 '22 at 14:45
  • @freedomn-m Just because they are asking for jQuery, doesn't mean it's best for them. It's more like someone asking where to find a pay phone to call a cab to get to store X but they don't know that the store is only 1000ft down the road so I reply by saying that they should just walk for 2 minutes in such direction instead of letting them waste their time. – Ruan Mendes Feb 04 '22 at 14:50
  • Thanks guys for your help and comments. I added some changes in my question as I foprgot to say that First I use custom IDs. But at least I have great insights from you. Cheers – Jacques tambard Feb 04 '22 at 17:20

1 Answers1

1
if($("#three").length)
{
    //it exists
}
Henryc17
  • 851
  • 4
  • 16