简体   繁体   中英

Remove parent element with Javascript using parentNode.removeChild when I only have 1 parent?

I'm working on a project where I have to delete en element in a certain condition (if). My code generates a <li> in which data is inserted via an array. I need to delete that <li> if some conditions are met.

Since removeParent() doesn't exist I've found different methods saying to use

e.parentNode.parentNode.removeChild(e.parentNode);

Rings a bell, it works in certains cases of course.

BUT

In my case, this is dynamically rendered elements in an array inside the <li> and there is no parent of the parent, so I get an error. My script looks for an element in the <li> , in my test I use a <div> found by its class.

$(node).find('.results-name')[0].parentNode.remove();

Does NOT work unfortunately so I'm looking for other ideas...

Any clue? Thanks a lot!

The thing is the code is huge and I can't copy/paste everything here.

Here's more info: - in the page I have a template that's used by the javascript to populate a div with all the contents. I have ~20 results. The template starts like this :

<script id="itemtemplate" type="geowacht/template">
    <div class="results-name">
        <h4 class="show-more-name itemaponaam" data-target="#result"></h4>
            <div class="d-block" id="result">
            <div class="itemaddress"></div>
            <div class="itemgeodesc"></div>
            <div class="itemphone"></div>
  • The pages calls a script that queries an API and returns 20 results. These results are parsed and added to the page using the template. Here's the beginning of the code:

apiconfig.default_populateItemNode = function(node, apotheekData, wachtPeriodeData, authenticationData) { $(node).find('.itemaponaam')[0].setAttribute('data-target', '#result-' + itemPos); $(node).find('.buttons')[0].setAttribute('id', 'result-' + itemPos + '-buttons'); $(node).find('.buttons')[0].setAttribute('data-apbnb', apotheekData.pharmacy.id);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM