how do I refresh a specific element in html using only vanilla javascript.
the goal is after adding an attribute to it with a value.
I will refresh the element.
how do I refresh a specific element in html using only vanilla javascript.
the goal is after adding an attribute to it with a value.
I will refresh the element.
Libraries like React are built for this purpose. But we can sort of do it with pure Javascript too. Here's what we need to do:
parent.removeChild(element)
document.createElement(element)
element.setAttribute(name, value)
parent.appendChild(newElement)
I have working example here in this link: https://codepen.io/edo9k/pen/poyRJJN?editors=1010
But you can also update the element directly, instead of 'refreshing' it. Which can be done with element.setAttribute
or getting the attribute directly like a I do changing the font size of the element in line 20 of the example: elm.style.fontSize = counter + "pt";
.
Your question lacks context, but depending on what you're trying to do, you should consider a library or some alternative method of updating this element. There might be security issues doing it like the way I'm proposing here, both by modifying attributes directly or by deleting/adding a new element.
You can find detailed explanation of all these API function in Mozilla's Web Docs: https://developer.mozilla.org/pt-BR/docs/Web/API