Hi everyone! I'm the author of code.mu :)
I'd love to chat with my non-Russian audience. I'm looking for your feedback on the site and the translation quality. Let's chat:)
⊗jqBsTECh 6 of 113 menu

Changing the text of an element using jQuery

In previous lessons, we learned how to get a group of elements via jQuery and even saw some examples of working with methods. Now let's look at them in more detail. In this lesson, we will learn how to change the text of an element using two methods: html and text.

Let's solve the following problem: get all elements with the class www and simultaneously change the text for all of them to '!!!'. Let's say we have the following HTML code:

<p class="www">text</p> <p class="www">text</p> <p class="www">text</p> <p>text</p>

Then the JavaScript code will look like this:

let elems = document.getElementsByClassName('www'); for (let i = 0; i < elems.length; i++) { elems[i].innerHTML = '!!!'; }

With jQuery, everything will be much simpler: get all the elements using the dollar sign, like this: $('.www'), and then apply the html method to them, which takes the new text as a parameter - $('.www').html('new text'). That's it! This line will do all the work without any loops or other nonsense. Run the example and see for yourself:

$('.www').html('!!!');

To all h3 using the html method, put the text '!!!'.

English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline