Possible is not in all browsers as they remove comments from tags. Commenting on the attribute is dangerous as it is necessary to modify the outerHTML
of its element, which ends up recreating/removing its elements (if they have a Javascript reference, they will only be killed, but can be declared on the page again with HTMLElement().appendChild
, etc..).
To better understand, test this snippet:
// Observação: em navegadores modernos, elementos com id
// são disponíveis em 'window' se tiverem caracteres válidos.
var minhaDiv = div;
document.body.innerHTML = '<div id="div">hey</div>';
// "old"
alert("Velha div: " + minhaDiv.innerHTML);
// "hey" - essa não é mais a 'minhaDiv'
alert("Nova div: " + div.innerHTML);
<div id="div">old</div>
Note: To comment on HTML is used <!---->.
Modifying an element’s "id" will only make a complication with its code.
Continuing, it would be better if you add/remove the attribute "id" with the value always memorized, for example:
// Você pode usar diretamente 'removeAttr' e
// 'attr' (com o id específico) para remover/adicionar o atributo id
// (portanto, é necessário
// jQuery.fn.each para percorrer cada elemento em uma função
// de toggle)
jQuery.fn.toggleAttr = function(attrName, value) {
this.each(function() {
// this é um elemento puro do JavaScript aqui, então:
// $(this) --> lista de elementos do jQuery
var $me = $(this);
// verifica se o atributo não existe (ou se é inativo)
if ($me.attr(attrName) === undefined || $me.attr(attrName) === false) {
$me.attr(attrName, value);
} else $me.removeAttr(attrName);
});
};
var $input = $("#meuid");
// memoriza o id
var id = $input.attr('id');
// remove o atributo id
$input.toggleAttr("id", id);
// adiciona o atributo id
$input.toggleAttr("id", id);
I know there is a way to remove $("p"). removeAttr("id");
– Fabiano Cacin Pinel
Yes, there really is. But I need you to just comment, because if you remove there is no way to return it.
– Pedro Lima
What if it renames? Does it? for example: id="meuid" for old-id="meuid"
– Fabiano Cacin Pinel
Interesting, how does it work ? @Fabianocacinpinel
– Pedro Lima
you would have to save the id value, remove the id and add the old-id attribute with the saved value, so I searched there is nothing ready to rename an attribute, but maybe you could solve by renaming the id, so id="@meuid" That would make it easy as $('#meuid'). attr('id','@meuid'); . Perhaps it would be easier for you to explain better what you do than to do. You should have another solution without having to do this.
– Fabiano Cacin Pinel
if you explain the reason for doing so, you may have a more appropriate solution.
– Daniel Omine