PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, October 15, 2022

[FIXED] how to append again to the position where I removed it?

 October 15, 2022     dom, javascript     No comments   

Issue

For example: here is to append between two paragraphs.

I want to keep it always appending where it was removed.

<p>Paragraph 1</p>
<div id="content">
  <h1>Content</h1>
</div>
<p>Paragarph 2</p>

function hide(){
  content.remove();
}

function show(){
  content.appendxxx();
}

Solution

What you can do is insert a comment placeholder to keep the place and use it when you want to put the element back.

let placeholder = document.createComment("placeholder");
let content = document.getElementById("content");

function hide(){
  if (content.parentElement) {
    content.parentElement.replaceChild(placeholder, content);
  }
}

function show(){
  if (placeholder.parentElement) {
    placeholder.parentElement.replaceChild(content, placeholder);
  }
}
<p>Paragraph 1</p>
<div id="content">
  <h1>Content</h1>
</div>
<p>Paragarph 2</p>

<button onclick="hide();">Hide</buton>
<button onclick="show();">Show</buton>



Answered By - TheGr8_Nik
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing