Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript hide/show element

Tags:

javascript

How could I hide the 'Edit'-link after I press it? and also can I hide the "lorem ipsum" text when I press edit?

<script type="text/javascript"> function showStuff(id) {   document.getElementById(id).style.display = 'block'; } </script>   <td class="post">    <a href="#" onclick="showStuff('answer1'); return false;">Edit</a>   <span id="answer1" style="display: none;">     <textarea rows="10" cols="115"></textarea>   </span>    Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum  </td> 
like image 974
nortaga Avatar asked Jun 05 '11 12:06

nortaga


People also ask

How do you hide and show elements?

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 display an element in JavaScript?

You can also use this code to show/hide elements: document. getElementById(id). style.

Can we hide HTML elements using JavaScript?

Using Css style we can hide or show HTML elements in javascript. Css provides properties such as block and none to hide/show the HTML elements.


2 Answers

function showStuff(id, text, btn) {      document.getElementById(id).style.display = 'block';      // hide the lorem ipsum text      document.getElementById(text).style.display = 'none';      // hide the link      btn.style.display = 'none';  }
<td class="post">    <a href="#" onclick="showStuff('answer1', 'text1', this); return false;">Edit</a>  <span id="answer1" style="display: none;">  <textarea rows="10" cols="115"></textarea>  </span>    <span id="text1">Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum</span>  </td>
like image 57
Sascha Galley Avatar answered Oct 15 '22 06:10

Sascha Galley


You can also use this code to show/hide elements:

document.getElementById(id).style.visibility = "hidden"; document.getElementById(id).style.visibility = "visible"; 

Note The difference between style.visibility and style.display is when using visibility:hidden unlike display:none, the tag is not visible, but space is allocated for it on the page. The tag is rendered, it just isn't seen on the page.

See this link to see the differences.

like image 21
A-Sharabiani Avatar answered Oct 15 '22 05:10

A-Sharabiani