1

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";
}
user4348719
  • 321
  • 1
  • 2
  • 12
  • You're not passing `el` to the function. `onclick="hide(this)"` supposed to do the trick. – Teemu Dec 12 '14 at 19:37
  • Now what? You've edited the question according to comment/answer, or fixed it to correspond the real code you have? – Teemu Dec 12 '14 at 19:39
  • no sorry I edited before you posted your comment, that was my bad, but its still not working. I realized that mistake before I checked the comments/answer to this question – user4348719 Dec 12 '14 at 19:40
  • 1
    Your code [seems to work](http://jsfiddle.net/pw5e5b4q/) ...? Is there something we are not seeing? (The confusion with the edit might be caused by my worm-degree connection ;) – Teemu Dec 12 '14 at 19:44
  • 1
    ok thanks! ill look into it more, now that I at least see that the function does work. Must be that where I have my javascript file is not properly linked to my html file? – user4348719 Dec 12 '14 at 19:46
  • Is the path of javascipt file correct? – user2019037 Dec 12 '14 at 19:46

2 Answers2

1

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> 
user2019037
  • 694
  • 1
  • 7
  • 14
0

Change you function:

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

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

Ansemo Abadía
  • 479
  • 3
  • 10
  • Change the function to what? The version you've in the answer is an exact copy of the function in the OP. Assigning the function to a variable doesn't change anything. Notice, that the fiddle I've linked in my comment is using function declaration, and it works fine. – Teemu Dec 12 '14 at 19:57
  • You need write the function like a variable assignation. The original example dont work: http://jsfiddle.net/9g7v3urn/ – Ansemo Abadía Dec 12 '14 at 20:08
  • You've "`onDomready`" set in the fiddle. OP has the script in `head`, which corresponds to "`No wrap-in `" at jsFiddle. You might want to read [this SO thread](http://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname). – Teemu Dec 12 '14 at 20:12