i have a code to add and delete of divs when i click add button div was creating and on delete button div was deleting but i need with single link when on first click div has to create on same link if we click on second time div has to delete please help me out with this issue

thanks in advance

8 Years
Discussion Span
Last Post by Taywin

One way can be that you can check if the div that is added dynamically exists or not by getElementById() funtion and than add or remove it.

Hope the sample code below helps you,

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  <TITLE> New Document </TITLE>
	function toggleDiv(wrapperDivName){
		//Check if the div with given id is present. If null than add the div else remove it
		if(document.getElementById(wrapperDivName+'Child') == null){//Add the div
			var newdiv = document.createElement('div'); //New div element
			var divIdName = wrapperDivName+'Child';
			newdiv.setAttribute('id',divIdName); //Set its id
			newdiv.innerHTML = 'Newly added div'; 
			document.getElementById(wrapperDivName).appendChild(newdiv); //Append to the wrapper div
		else{//Remove the div
			var removediv = document.getElementById(wrapperDivName+'Child'); //div element to remove
			document.getElementById(wrapperDivName).removeChild(removediv); //Remove from the wrapper div

 <INPUT TYPE="button" VALUE="Add/Remove Div" ONCLICK="toggleDiv('wrapper')">
 <DIV id="wrapper">


I'm not sure I understand. Do you want it to toggle? Do you have some sample code?

say I have post-1,post-2............post-112

so home page will show post-102 to post-112

if new post is published as post 113
automatically it will shot post-103 to post-113.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.