I have an anchor tag. I want to hide/show it dynamically. I can’t put it inside a div /span for some reason. How can i do it?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Once you have a reference to the DOM element for the anchor tag, do this:
There are several ways you’d get that reference. For instance, if the anchor has an
id, you can usegetElementById:HTML:
JavaScript:
If the anchor doesn’t have an ID and you don’t want to add one for whatever reason, you can use other information about it to identify the correct element. This is most easily done with a library like jQuery, Prototype, YUI, Closure, or any of several others that will give you the ability to use CSS selectors to find the anchor(s) in question. Without a library, you have to rely on a small number of well-supported DOM functions or deal with browser variations, which is what the libraries help you avoid.
Here’s an example (live copy) of looping through all of the anchors on a page and hiding one based on its
href:HTML:
JavaScript:
That gets dramatically easier if you use a library. For instance, here’s a jQuery version of it (live copy):
Here’s the Prototype version (live copy):