Reference DOM element in Jquery otherwise

Asked

Viewed 173 times

1

Hello, look at these lines of code:

 <div id="divn4" align="center"><i style="">Nunca, nunca enviamos span.</i></div>

So, if I wanted to reference the italic there, to apply some function to it, without having to put an id, or class, in Jquery, how can I do it? I think of something like:

$("#divn4[i]").text("teste");

In short, it would be something like in CSS, where you can create a selector not necessarily using an ID or Class..

1 answer

3


The i is an HTML tag in the same way that p or div, and hence a valid CSS selector, so the solution is:

$("#divn4 i").text("teste");

If you want to change all italics you can really use $("i")

Example:

$("#divn4 i").text('Texto dentro do "i" aqui.');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="divn4" align="center">Texto fora do "i" aqui. <i style="">Nunca, nunca enviamos span.</i></div>

  • Perfect, I didn’t have this idea that it was in the same way the specificity applied there in CSS, it was also in javascript(jquery), grateful partner, even more.

  • 1

    @Alexandrec.Caus The jQuery (and pure JS selectors with querySelector and querySelectorAll) are the same as the CSS, save one or other extension.

Browser other questions tagged

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