On Apr 24, 7:47 am, jpd...@gmail.com wrote:
Hi all,
I'll trying to tell the difference between the following three cases:
<img alt="text string" />
<img alt="" />
<img />
I can do this in Firefox with the following code, where elem is the
HTMLElement representing each image, but IE doesn't seem to
differentiate between empty string and undefined.
var alt = elem.getAttribute('alt');
alt = (alt) ? alt : ((alt===null) ? 'really_null' : "");
The desired output from running this code on the 3 tags above is:
text string
really_null
It seems like this should be really easy...but I'm having a really
difficult time trying to figure out what's going on...
Thanks!
Jeff
What you want to check for first is whether or not it's the value is
null. If it's not, then you need to check to see if the value is "".
If it's not, then all should be great. I think the only time it will
be set to null, is if you've explicitly set it to null within your
script. May I ask why you need to know? Generally, you only need to
get the value, which is either a string, or nothing. If it's nothing,
then there's not much more you can do unless you set it to something.
This script should illustrate what you might want to do. Obviously, it
will require modifying, but I think it demonstrates what is happening.
<html>
<head>
<script type="text/javascript">
function getAlt(id) {
alert(id+" =
"+document.getElementById(id).getAttribute("alt")) ;
}
window.onload = function() {
getAlt("img1");
getAlt("img2");
getAlt("img3");
}
alert("blah");
</script>
</head>
<body>
<img id="img1" src="http://www.google.co.uk/intl/en_uk/images/
logo.gif"><br />
<img id="img2" src="http://www.google.co.uk/intl/en_uk/images/
logo.gif" alt=""><br />
<img id="img3" src="http://www.google.co.uk/intl/en_uk/images/
logo.gif" alt="Google UK Logo">
</body>
</html>