Jump to content

[SOLVED] Displaying + removing HTML code


Andy17

Recommended Posts

Hey guys,

 

I would like to have a script that displays some HTML on the page when a text link is pressed and then removes it when you click the link again. Displaying the HTML wouldn't really be a problem, but I am not sure how to remove it again. Could anyone please help me out with some code?

 

Thank you in advance! :)

Link to comment
Share on other sites

very easy

 

first set up the area in which you want to add/remove html

 

<div id="add_remove_area"></div>

 

set up your link

 

<a href="#" id="add_remove_link" >add remove html</a>

 

now your js is very simple. in the head add this code

 

<script>

onload = function(){

var link = document.getElementById('add_remove_link');

var container = document.getElementById('add_remove_area');

var status = false;

var content = 'your html here';

 

link.onclick = function(){

status = status ? false : true;

container.innerHTML = status ? '' : content;

};

};

</script>

 

i didnt test that, but it should work

Link to comment
Share on other sites

Hello emehrkay and thanks for your help.

 

I had to change the variable "status" to true or else I would have to click twice for the HTML to show. :) Anyways, since the JS code is in the head and the function is called from the body, the HTML is displays above my text link. Is it possible to display it below the link?

 

Thank you in advance! :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.