Web Analytics Made Easy -
StatCounter find cell class name(s) - CodingForum

Announcement

Collapse
No announcement yet.

find cell class name(s)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • find cell class name(s)

    How can you find an attribute - more specifically the class of cell in a <table>? In raw JS please.
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

  • #2
    Do you already have access to the cell? Then it's just Element.className, though if you're mixing XML with HTML (such as SVG) that can become unreliable, in which case use Element.getAttribute('class'). Element.classList could also be used, but remember it doesn't exist in older UA's... also remembering that className and the getAttribute returns them as a single space delimited string, whilst classList is an Array-like / iterable.

    Keep in mind too that whilst classList can be convenient, it is a JS array-like which in reality are pointered lists, which is why they're painfully slow compared to "real" arrays in other languages. Sometimes it's even faster to regex the className or attribute string than it is to use anything classList does.

    That said how are you going to get hold of said cell to get the attribute from it in the first place? Are you using HTMLTableRow.cells? Are you grabbing it by ID? Are you walking the DOM or using an event.currentTarget?

    An example table and more information on what/why you're trying to do this could help dial in the answer better. It's hard to give a proper example code without that.
    I'll kill you and your dreams tonight, begin new life.
    Bleed your death upon me, let your bloodline feed my youth.
    https://cutcodedown.com

    Comment


    • #3
      Thank you DS. Why did I forget this? Used Element.getAttribute('class') and things worked out fine.
      Evolution - The non-random survival of random variants.
      Physics is actually atoms trying to understand themselves.

      Comment


      • #4
        Originally posted by sunfighter View Post
        Thank you DS. Why did I forget this? Used Element.getAttribute('class') and things worked out fine.
        I switch between languages so frequently I forget stuff all the time. It's part of why I say good research skills and a quality language reference are more important than rote memorization.

        Basically MDN is your friend.

        Also, this stuff has changed recently with the addition of Element.classList. It's good to know it's there -- an array-like of classes on an object rather than a flat string, with specific methods for handling classes. It's slow, it doesn't exist in older IE's, but it replaces a lot of the extra 'helper function' and stuff frameworks used to handle.

        Mind you, if I'm doing heavy class manipulation I actually still use regex, as astoundingly regex is still faster than JS array-likes... but it's good to know it's there:

        https://developer.mozilla.org/en-US/...ment/classList

        The add, remove, and toggle methods are something that really should have been in JS from day one... and being an array-like you can use the "contains" or "indexOf" to find if classes exist in it.

        Even though I still prefer working with the attribute string in larger codebases, it's handy in smaller ones to not have to load up a bunch of junk just to toggle a class on and off. It's just another tool in the toolbox.

        That's one of the big obstacles for any of this stuff, not only is it remembering all this stuff, it's that there's new stuff added all the time. It's why I tell beginners all the time NOT to focus on the details of how each and every function exists, each and ever parameter, yadda, yadda. Just be aware that it exists and can be done, and when you need the details go to the specification or a really good reference. The best programmers out there don't know everything off the top of their head, nobody can actually do that, and those who say otherwise are lying to stroke their own... ego. I mean something else, but let's say ego.

        So don't feel bad about forgetting something simple like this. I had to delve into one of my Ada codebases last week, and I ended up having to drag out the old-school books on it as there's no decent online reference. I spent a good chunk of the '90's working in the language, but in the decades since I've forgotten more than most will ever know. It happens.

        Just be happy I answered before someone came in with a snarky asshat "don't you know how to Google?"
        I'll kill you and your dreams tonight, begin new life.
        Bleed your death upon me, let your bloodline feed my youth.
        https://cutcodedown.com

        Comment

        Working...
        X