Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8486371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:55:42+00:00 2026-06-10T20:55:42+00:00

I am using javascript and jQuery to dynamically add and delete new elements to

  • 0

I am using javascript and jQuery to dynamically add and delete new elements to and from an existing <div>.

Adding a 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.

Javascript

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");   
  newElement.innerHTML = ab;
  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);
}

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

Html Code

<div id="contControls">
  <div class="optionPane">
    <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>
  </div>
</div>

I’m not able to get the <div> element having class optionPane; whenever I try to target the parent of the <table> I get the <div> with the id contControls.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T20:55:44+00:00Added an answer on June 10, 2026 at 8:55 pm

    Working Demo

    If you’re going through the trouble of importing jQuery, you may as well use it. It simplifies all of the DOM manipulation that you are trying to accomplish, and your code boils down to simply this (note that javascript string can’t span multiple lines like you are doing without using + for concatenation):

    var ab = '<table rules="none" width="100%">'
     + '<tr><td class="optHdBg"><a>'
     + '<img src="themes/theme_blog/images/icons/delete.png" name="delete" />'
     + '</a></td></tr></table>';
    
    $(document).ready(function() {
        $(document).on('click', 'img[name="delete"]', function() {
            remove_block(this);
        });        
    });
    
    function add() {
        $("<div class='optionPane'></div>").html( ab ).appendTo("#contControls");
    }
    
    function remove_block(obj) {
        // remove the entire optionPane <div>
        $(obj).closest('div.optionPane').remove();
    }​
    

    Also, you should note that in the line $(document).on('click' you can (and should) replace document with whichever static parent element you are adding the dynamic elements to (probably #contControls, but I can’t see your HTML); this provides better performance, because events don’t have to bubble as far up the DOM.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using javascript to dynamically add new tabs in jquery. I use the following
I have a problem with dynamically added script element (using jQuery). Code for adding
I'm using jQuery 1.5.2 trying to dynamically load a div from one source into
I'm using jQuery to add elements dynamically to a dropdown but I think I'm
I am adding elements dynamically using javascipt: $('.marker').append('<div class=container id=id' + counter + '
I need to dynamically build a list of textboxes using javascript and jquery. The
When trying to dynamically load a Javascript file using jQuery I keep getting a
Is it possible to get the top position of an element using javascript/jquery ?
I want to append a div tag generated by jQuery dynamically with a javascript
I'm using jQuery to dynamically add two divs on page load. It all works

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.