Set attributes of tags of HTML using JSoup

html, java, jsoup, tags

Solution

You cen do this with `attr()` method in both ways: loop or directly on the `Elements` object:

// In a loop
for( Element img : doc.select("img[src]") )
{
    img.attr("src", "your-source-here"); // set attribute 'src' to 'your-source-here'
}

// Or directly on the 'Elements'
doc.select("img[src]").attr("src", "your-value-here");

In fact both solutions are the same.

Problem

How to set attributes of tags of HTML using JSoup? I want to set the attribute->"src" of tag->"img" in Java using Jsoup Library. ``` Elements img_attributes = doc.select("img[src^=/im]"); for(Element img_attribute: img_attributes) { String s = img_attribute.attr("src"); System.out.println(s); } ``` This code prints the `src` values. I want to change the `src` values.

Original source