Javascript, an id that was null (empty) can be returned?

1

I need to know if it is possible to return a id that was removed, because I want to get the same and remove the id="", and then click on a button / p>

(Target)

1 • Click button and remove √

2 • I click on another button and it returns X (since it was to null I can not:)

    
asked by anonymous 07.02.2018 / 19:54

1 answer

0

If you remove the id from the element, you will not be able to retrieve it, unless you save it somewhere, but that would be tricky if it's multiple elements.

You can use other methods to keep the% w / o of the element if you need to recover it in the future. One of them is to use dataset , creating a id attribute, eg:

<a id="" data-id="foo" href="">link</a>

When I delete the data-id of the element, I can then pull it by picking up the dataset :

function teste(){
   
   var elemento = document.querySelector("a");
   
   elemento.id = elemento.dataset.id;
   
   //somente para ilustrar
   elemento.innerHTML = "id recuperado!";
}
<a id="" data-id="foo" href="">id vazio</a>
<br>
<button onclick="teste()">Recuperar id</button>
    
07.02.2018 / 20:12