I have a set of elements <a>
that have, for example:
<a class="menu-elemento" href="#">Hola</a>
<a class="menu-elemento" href="#">Mundo</a>
And I want to add text to the existing one. So that they are like this:
<a class="menu-elemento" href="#">*** Hola</a>
<a class="menu-elemento" href="#">*** Mundo</a>
Where ***
they represent the text I want to insert. How can I achieve this using jquery or native javascript?
As you have two elements with the same class, to select each one you can use the eq selector , on the other hand to insert the text you can use the prepend function
If the text is the same for all, you can use what @LeonardoCabré says, although it is not necessary to do it one by one. You just do it using the class
menu-elemento
which I'm assuming is the same one shared by the group elements:If you want it in JavaScript you can use
getElementsByClassName
to get the elements by their class, iterate over them and add the text to the beginning:You must use selectors to be able to access the DOM elements, there are different types of selectors are those of CSS classes, identifier, by label, etc, that way you can access the attributes of each , I give you a dynamic example using the value of a text box, to put it at the beginning of each
if you are new I recommend that you start by learning how to access elements within the DOM