In this code:
<img id="SizeImage1" style="cursor:pointer">
As you can see, it has no attribute src
. How can I tell if a tag has src
or not using JavaScript?
In this code:
<img id="SizeImage1" style="cursor:pointer">
As you can see, it has no attribute src
. How can I tell if a tag has src
or not using JavaScript?
You can check using the hasAttribute () method, see below:
document.getElementById('SizeImage1').hasAttribute('src');
It will return true or false .
More information here: W3Schools - hasAttribute .
Another way is to get the value of src
, then check whether it is empty or not:
var src1 = document.getElementById("SizeImage1").src
, src2 = document.getElementById("SizeImage2").src;
// verifica se está null, '', undefined, 0
if (!src1)
console.log("Primeiro está vazio");
if (!src2)
console.log("Segundo está vazio");
<img id="SizeImage1" style="cursor:pointer">
<img id="SizeImage2" style="cursor:pointer" src="google.com">