⊗jqDmAtt 32 of 113 menu

Working with Attributes in jQuery

Now we will learn how to read, add and change HTML tag attributes via jQuery. All this is done using the method attr. It works like the method html - depending on the number of parameters, it will read or change the attribute value.

The first parameter of attr takes the attribute name, and the second one takes its new value. For example, like this - .attr('value', 'www') - we will write the string 'www' into the attribute value.

If we call attr with one parameter, it will return the value of the attribute whose name we specify in the first parameter. For example, like this - .attr('value') - we will get the value of the attribute value.

In the following example we have an input:

<input type="text" value="!!!" id="test">

Next we get the input by its id and display the value of its attribute value:

alert($('#test').attr('value')); // will bring out '!!!'

Get the contents of the src attribute of an image given its id, which is www.

Get the href attribute of the link contained in the element with class www.

Get the contents of the src attributes of all images on the page and output them sequentially.

Get the href attribute of all links and output them sequentially.

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