paco paco - 1 year ago 34
HTML Question

Return the string based on the source via Javascript

I have a img source on my page with no tag id to select it and would like to regex match to it and return the full URL.

The img looks like this:

<div style="display: none;">
<img src=""style="display:none" width="0" height="0">

I have tried the following JS:

var allImages = document.getElementsByTagName("img");
var target;
for(var i = 0, max = allImages.length; i < max; i++)
if (allImages[i].src =~ ""){
target = allImages[i];

what I would like to do is have a script to iterate through all images and if the scr url contains suburl2, return the full src url.

hopefully that all makes sense.... thanks for your help

Answer Source

You can use document.querySelector() within attribute equals, attribute begins with or attribute contains selector to select <img> element having src attribute equal to ""

 var img = document.querySelector("img[src='']");
<div style="display: none;">
  <img src="" style="display:none" width="0" height="0">