java - JSoup - Add onclick function to the anchor href -


existing html document

<a href="http://google.com">link</a> 

like convert as:

<a href="#" onclick="openfunction('http://google.com')">link</a> 

using jsoup java library many fancy parsing can done. not able find clue add attribute above requirement. please help.

to set attribute have @ the doc

string html = "<html><head><title>first parse</title></head>"               + "<body><p>parsed html doc.</p><a href=\"http://google.com\">link</a></body></html>"; document doc = jsoup.parse(html);     elements links = doc.getelementsbytag("a"); (element element : links) {     element.attr("onclick", "openfunction('"+element.attr("href")+"')");     element.attr("href", "#"); }  system.out.println(doc.html()); 

will change :

<a href="http://google.com"> 

into

<a href="#" onclick="openfunction('http://google.com')">link</a> 

Comments