Javascript, a null(empty) id can be returned?

Asked

Viewed 29 times

1

I’m wondering if it’s possible to return one id which has been removed as I want to get the same and remove the id="", and then click on a button for example, to put back what was before.

(Goal)

1• I click button and remove

2• I click on another button and return X (since it went to null I cannot :)

1 answer

0


If you remove the id of the element, it will not be able to recover it, unless you store it somewhere, but it would be complicated if there are several elements.

You can use other methods to maintain the id in case you need to recover it in the future. One of them is to use dataset, creating an attribute data-id, ex.:

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

By erasing the id of the element, I can pull back by 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>

Browser other questions tagged

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