user4348719
user4348719

Reputation: 351

Using Javascript to hide an element or specific id in html

Hello I wanna write a javascript function that hides an element/specfic id in my html code once the user clicks on that text. What I have right now isn't hiding the html. I'm not so good at javascript so I appreciate any help

what i have for my html:

<head> <script type="text/javascript" src="hide.js"></script> </head>

<h1 onclick = "hide(this)">[Name]</h1> 

What I have for my javascript function:

function hide (el) {
    el.style.visibility = "hidden";
}

Upvotes: 1

Views: 99

Answers (2)

Ansemo Abad&#237;a
Ansemo Abad&#237;a

Reputation: 488

Change you function:

hide = function(el) {
   el.style.visibility = "hidden";
}

You can test here: http://jsfiddle.net/wzte1ru9/5/

Upvotes: 0

user2019037
user2019037

Reputation: 762

You must pass this to the javascript function as parameter:

<h1 onclick = "hide(this)">[Name]</h1> 

this represents the HTML DOM element.

function hide(el) {
  el.style.visibility = "hidden";
}
<h1 onclick="hide(this)">[Name]</h1> 

Upvotes: 1

Related Questions