Click here to Skip to main content
15,896,153 members

add and remove child elements

Manish Kumar Namdev asked:

Open original thread
Hello Friends,

I am working with HTML,javascript and jquery in my code, dynamically add and delete the new elements to the existing "div".

Adding new element is working fine, but when I click on delete to remove the element, I get the Main Container object and also the child element object within the button exists and was clicked.

now the problem comes when I try to delete the element, I am getting the object till the html table control that contains the controls, but do not get the div that is containing the table.

Note : the div contains the table is the child div/element.

To Add element.
var ab = '<table rules="none" width="100%">
<tr>
<td class="optHdBg">
<a><img src="themes/theme_blog/images/icons/delete.png" name="delete"  önclick="javascript:remove_block(this);"/></a>
</td></tr></table>';

function add()
{
var lstChild = $("#contControls").children().last();
var containerElement = document.getElementById("contControls");	
var newElement = document.createElement("div");
$(newElement).addClass("optionPane");
document.getElementById("contControls").appendChild(newElement);
}

function remove_block(obj)
{
  var mainContainer = $(obj).parents("div #contControls");
  var mySelf = obj.parentNode.parentNode.parentNode.parentNode.parentNode;
  mainContainer.removeChild(mySelf);
}


problem occurs with mySelf control that is not accessing the div control that contains the table, due to that delete operation doesn't work.


please guide me.
Thanks.
Tags: Javascript, HTML, jQuery

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900