Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

Javascript - differentiating between NULL and empty alt attributes

How can I find/change a script to discriminate between NULL and empty.



12:58 pm on Apr 8, 2003 (gmt 0)

10+ Year Member


How do we discriminate between:

<IMG SRC=".." ALT=""> (empty ALT tag)
and <IMG SRC=".."> NULL alt tag.

Is this possible and how?



3:47 pm on Apr 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

I have had a play and a search for you, but as I suspected, without any luck... so as far as I know there is no way of knowing the difference using JS.

According to this article on MSDN [msdn.microsoft.com], I think you should be able to... I haven't managed to get it to work yet though.

- Return Value [of the getAttribute() method]
Variant that returns a String, number, or Boolean value as defined by the attribute. If the attribute is not present, this method returns null.

Maybe if you tell us what you're trying to do with this information we can still help you out?


4:22 pm on Apr 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

The problem with getAttribute() is that it appears to know that ALT is a valid attribute name for IMG and so returns "" (this behaviour is helpful in most cases). If you invent a new attribute name eg XATT then it returns "" and null depending on whether it is present.

All I can think of is to check the actual HTML like
if (img.att=="" && indexOf(img.outerHTML, "ALT="))


4:50 pm on Apr 8, 2003 (gmt 0)

10+ Year Member

What I would like to achive is to adjust the following javascript favelet to not just show ALT tags when they are provided by also highlight when ALTs are not declared (but not when asigned ="".

javascript:(function(){function toArray (c){var a, k;a=new Array;for (k=0; k<c.length; ++k)a[k]=c[k];return a;}var images, img, altText;images=toArray(document.images);for (var i=0; i<images.length; ++i){img=images[i];altText=document.createTextNode(img.alt);img.parentNode.replaceChild(altText, img)}})();


[edited by: tedster at 6:55 pm (utc) on April 8, 2003]


Featured Threads

Hot Threads This Week

Hot Threads This Month