Difference between empty attribute and undefined attribute

J

jpdogg

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
 
J

Janwillem Borleffs

(e-mail address removed) schreef:
It seems like this should be really easy...but I'm having a really
difficult time trying to figure out what's going on...

This is actually per DOM specifications: if the attribute doesn't exist,
an empty string should be returned instead of null:

http://developer.mozilla.org/en/docs/DOM:element.getAttribute

(scroll down to the Notes)

For IE, however, you can apply the following workaround:

<img />

alt = document.getElementsByTagName('img')[0].attributes['alt'];
value = alt && alt.specified ? alt.value : 'really_null';
alert(value) // really_null


JW
 
D

Daz

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>
 
D

Daz

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

Whoops. Sorry. I missed the part about there being a problem with IE.
I'm still working on this for you.
 
D

Daz

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

Sorry, it would appear that Microsoft have asked that same question
that I did. Why do you need to know if it's null or "". Surely you
only need to know whether the value is an empty string, or a string
containing text?

Sorry I couldn't help more.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top