lets say i have string something like
x = '<div class="sample">
<img src="http://www.example.com/i/java.png">
</div>
<div class="sample_another">
<img src="/i/somedir/python.png">
</div>'
i have 2 functions with me
getheight() => returns img height and getwidth() => returns img width
I want to add the width and the height attribute to the img tags whose value i can get it from those functions. i couldnt find regex for this so i was thinking more of a general approach.
This is what i came up with so far
var string ="<img alt='' src='http://api.com/images/UID' /><br/>Some plain text<br/><a href='http://www.google.com'>http://www.google.com</a>";
var elem= document.createElement("div");
elem.innerHTML = string;
var images = elem.getElementsByTagName("img");
for(img in images){
...
}
but after i dont know how to proceed as this is converted from dom elements to string. and i still have to append height and width.
my final string should be something like this
x = '<div class="sample">
<img src="http://www.example.com/i/java.png" height="200px" width="100px">
</div>
<div class="sample_another">
<img src="/i/somedir/python.png" width="150px" height="150px">
</div>'
can anyone help me achieve this please. Thanks