Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you add/remove hidden in <p hidden> with JavaScript

How do you add and remove 'hidden' from <p hidden>My Text</p>?

I tried removing the attribute and setting it to false but neither of them worked.

  let p = document.getElementsByTagName('p');
  let myText;
    
  for (i = 0; i < p.length; i++) {
    if (p[i].innerHTML == "My Text") {
      myText = p[i];
      break;
    }
  }

  myText.removeAttribute("hidden"); // no effect
  myText.setAttribute("hidden", false); // no effect
like image 544
Skywarp Avatar asked Apr 14 '18 21:04

Skywarp


People also ask

How do I unhide a hidden tag in HTML?

document. getElementById("button"). hidden = "false"; As hidden is not a css visibility : hidden; property.

How do you make an element invisible in JavaScript?

Style display property is used to hide and show the content of HTML DOM by accessing the DOM element using JavaScript/jQuery. To hide an element, set the style display property to “none”. document. getElementById("element").

How do you add a hidden in HTML?

<input type="hidden"> <input> elements of type hidden let web developers include data that cannot be seen or modified by users when a form is submitted.

How do I hide the P tag in HTML?

Use display:none to hide an element, it will not be available in the page and does not occupy any space. Use visibility:hidden to hide an element, it will still take up the same space as before when visible.


2 Answers

Could you set an ID on the <p> tag and interact with it that way?

<p id="whatever" hidden>My Text</p>

And:

let p = document.getElementById('whatever');
p.removeAttribute("hidden");
like image 60
John Luscombe Avatar answered Sep 19 '22 06:09

John Luscombe


It looks fine here. Try with this code if you wish.

index.html

<html>
<head>

</head>
<body>
      <p hidden>My Text</p>
</body>
</html>

script

let p = document.getElementsByTagName('p');
let myText;

for (i = 0; i < p.length; i++) {
  if (p[i].innerHTML == "My Text") {
    // console.log(myText, p[0].innerHTML);
    myText = p[i];
    break;
  }
}

myText.removeAttribute("hidden"); 

You can see in codePen https://codepen.io/anon/pen/qozVaq

like image 28
Wesley Gonçalves Avatar answered Sep 19 '22 06:09

Wesley Gonçalves