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

Recommended Answers

All 4 Replies

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">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
 <SCRIPT LANGUAGE="JavaScript">
 <!--
	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
		}
	}
 //-->
 </SCRIPT>
 </HEAD>

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

 <DIV>
 </BODY>
</HTML>

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

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.

Almost 2 years old post...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.