0

I'm limited to one line because of a Chrome Extension and it's the only one that fits my needs.

The <td> needs to be gone through to the attribute "UserName" to determine if a blocked user needs to be invisible.

I cannot figure this out and I'm really a noob when it comes to Javascript (not my language)

I've tried display:none hidden and style.visibility="hidden".

I've tried w3Schools and searched through Javascript and HTML pages for how to this and while I've got code that does work, it's a script that takes about 8 lines which doesn't work. I may have to ditch it but I figured I've give it one last shot.

document.getElementsByTagName("td")[0].getAttribute("theUserName").value("madmax").style.visible = "hidden";

Expected - The <td> should not show up Results - It shows up

Emeeus
  • 5,072
  • 2
  • 25
  • 37
  • 1
    You simply cannot accomplish this with one statement because property access doesn't return a reference to the DOM element for a second property to be accessed (chaining). Also, attributes don't have a `.value` property and even if they did, `value` is a property, not a method, so it can't take an argument. Additionally, `td` elements don't have a `theUserName` attribute. There is just so much wrong with what you are doing here. – Scott Marcus Jul 25 '19 at 18:04
  • Damn, and thanks for the edit. Can you do it in two? The extension does not like variables but I can give it a shot - it's Codify - The Code Adder. – Little John Jul 25 '19 at 18:07
  • Doesn't make sense that you are limited to only one line – charlietfl Jul 25 '19 at 18:07
  • Please show us a sample of the HTML that you are accessing with this code and explain what you need to accomplish. – Scott Marcus Jul 25 '19 at 18:08
  • And yes - I know, it's horrible. But I'm doing my best dealing with a bunch on non-coders. – Little John Jul 25 '19 at 18:08
  • `document.getElementsByTagName("td")[0]` [is just a bad idea... always](https://stackoverflow.com/questions/54952088/how-to-modify-style-to-html-elements-styled-externally-with-css-using-js/54952474#54952474). Use `document.querySelector("td")` instead. – Scott Marcus Jul 25 '19 at 18:11
  • Will try that, Scott :) – Little John Jul 25 '19 at 18:34
  • Charlietfl - Supposedly I can have more but code from here does not work in the extension. Evidently he's had complaints. The alternative would be Java-Injector but obviously I am javascript-inexperienced - assuming the java does not actually mean Java. – Little John Jul 25 '19 at 18:42

2 Answers2

2

One line to hide the content:

[...document.querySelectorAll("td[theUserName=madmax]")].forEach(e=>e.style.display = 'none');

To remove the <td>:

[...document.querySelectorAll("td[theUserName=madmax]")].forEach(e=>e.remove());
Emeeus
  • 5,072
  • 2
  • 25
  • 37
1

I am assuming that you to iterate through all <td> elements to search for one with theUserName attribute with value madmax, then make that element invisible. That can be achieved with:

for(TdElement of document.getElementsByTagName("td")) {
  if (TdElement.getAttribute("theUserName") == "madmax") {
    TdElement.style.visiblity = "hidden";
  }
}

Condensed to a single line, this is:

for(TdElement of document.getElementsByTagName("td")) if TdElement.getAttribute("theUserName") == "madmax") TdElement.style.visiblity = "hidden"

Ian
  • 329
  • 1
  • 11