I have created 2 buttons in HTML, they have their id and name , I need to make them get their text from the name button , I think that for this I have to pass the id and name button from HTML to JavaScript and from JS generate the button text in the HTML file, but I don’t know how to do it. In any case, I will be very grateful for the help.
|
2 answers
After loading the DOM tree, execute the following code:
Javascript:
var el = document.querySelectorAll('button'); for (var i = 0; i < el.length; i++) { el[i].textContent = el[i].getAttribute('name'); } <button id="id1" name="test1"></button> <button id="id2" name="test2"></button> - Thank you brother, it helped a lot. - Alisa Bondarchuk
- @AlisaBondarchuk, if this answer is the answer to your question, then tick it with a tick to the left of the answer. In the future, this may help people find a similar question. - meine
- Ok, thanks! - Alisa Bondarchuk
|
In that case, if you don’t have to use JS, you can get by with the :before pseudo-element and pull the necessary attribute into content .
button:before { content: attr(name); } <button id="id1" name="test1"></button> <button id="id2" name="test2"></button> - It seems that CSS will replace JS soon, it is already a lot, where it replaces it. And the most important thing is that I even like it.)) - Sonic Myst
- @SonicMyst this technique is not new at all. But, yes, now with the help of css you can realize a lot of things without using JS. - Alexey Giryayev
- Thank you brother for help, just demanded to do it with js - Alisa Bondarchuk
- oneNo problem. That's why I wrote that this is an alternative to JS in this example) - Alexey Giryayev
|
nameattribute in Misha Saidov