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";
}
6
  • You're not passing el to the function. onclick="hide(this)" supposed to do the trick. Commented Dec 12, 2014 at 19:37
  • Now what? You've edited the question according to comment/answer, or fixed it to correspond the real code you have? Commented Dec 12, 2014 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 Commented Dec 12, 2014 at 19:40
  • 1
    Your code seems to work ...? Is there something we are not seeing? (The confusion with the edit might be caused by my worm-degree connection ;) Commented Dec 12, 2014 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? Commented Dec 12, 2014 at 19:46

2 Answers 2

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> 

Sign up to request clarification or add additional context in comments.

Comments

0

Change you function:

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

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

3 Comments

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.
You need write the function like a variable assignation. The original example dont work: jsfiddle.net/9g7v3urn
You've "onDomready" set in the fiddle. OP has the script in head, which corresponds to "No wrap-in <head>" at jsFiddle. You might want to read this SO thread.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.