How to unlink object elements?

Asked

Viewed 107 times

2

I have the following function

addPerson = function(id, name) {
    people[id] = name;

    console.log(people[id].parentNode); // Exibe corretamente
    console.log(name.parentNode); // Exibe corretamente

    name.parentNode.removeChild(name);

    console.log(people[id].parentNode); // Deixa de exibir
    console.log(name.parentNode); // Deixa de exibir

    if(!name.previousElementSibling) {
        $(name.parentNode).append(name);
    }else{
        name.previousElementSibling.insertAfter(name);
    }
};

--

The parameter name reference an element in DOM, when the function is called people[id] receives this element, but when executing some example delete element change, the reference loses access to methods, example the parentNode, tried to use the clone of J-query, but it didn’t work.

Is there any way to clone the object, keep the native methods and detach it from the element in the DOM?

  • So instead of vector you could use a list, I think it would be more viable in your case.

1 answer

2


You don’t need to clone the element unless you need multiple equals.
You can use var semPai = el.parentElement.removeChild(el); and you get the "in hand" element out of the DOM.

var div = document.querySelector('div');
var btn = div.firstElementChild;
btn.addEventListener('click', function() {
  console.log('Clicado!');
});

var semPai = btn.parentElement.removeChild(btn);
console.log(div.children.length); // vazio
console.log(semPai.parentNode); // sem pai!

btn.click();
<div><button>Clica-me</button>
</div>

If you need to know the initial position I suggest you create an object to store this information like this:

var objs = [];

var div = document.querySelector('div');
var btn = div.firstElementChild;
btn.addEventListener('click', function() {
  console.log('Clicado!');
});

// guardar para mais tarde
objs.push({
  el: btn,
  previousSibling: btn.previousElementSibling || {},
  parent: btn.parentElement
});

btn.parentElement.removeChild(btn);
console.log(div.children.length); // 0 => vazio
console.log(btn.parentNode); // null => sem pai! está fora do DOM

// depois mais tarde...
objs[0].parent.insertBefore(objs[0].el, objs[0].previousSibling.nextSibling);
<div><button>Clica-me</button>
</div>

  • I still can’t, even assigning directly to a variable, I’m using a library I don’t know if it has anything to do with.

  • @Felipeduarte can you describe more about what the application should do? Don’t worry about the implementation, explain what you need in the program

  • It is a do and undo system, I am using this https://github.com/ArthurClemens/Javascript-Undo-Managerlibrary, when accessing the method I need to delete the element (if it exists) and put it back in the same position it was in

  • @Felipeduarte ok. And how do you know where it was? You wanted the element to keep it even though it was outside the DOM, that’s it?

  • It’s just that I’m doing tests, everything will definitely be created dynamically, for now I’m going straight to the DOM. when the redo method is triggered, the element stored in the vector, return to the DOM in the same position, with previousElementSibling would be able to put it in the correct position, but the method adds.

  • @Felipeduarte added a suggestion to the answer

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.