I want to loop through each element by its class for setting new value in the src attribute. below is the jquery code, how can i rewrite in typescript in angular
$(".image").each(function () {
$(this).attr("src","http://xxx/xxx.png");
});
I want to loop through each element by its class for setting new value in the src attribute. below is the jquery code, how can i rewrite in typescript in angular
$(".image").each(function () {
$(this).attr("src","http://xxx/xxx.png");
});
Try to use opposite approach - store collection inside component and iterate inside view:
component.ts
public urls = ['url1', 'url2'];
component.html
<img *ngFor="let url of urls" [src]="url">
If you change something in your urls
now it will affect the view.
I think this may help you:
Array.from(document.getElementsByClassName("image")).forEach(function(item) {
var image_link=item.getAttribute('href');
item.setAttribute('src',"path");
});
Please check this for details: For loop for HTMLCollection elements