473,506 Members | 17,266 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Difference between empty attribute and undefined attribute

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

Apr 24 '07 #1
4 4652
jp****@gmail.com 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...t.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
Apr 24 '07 #2
Daz
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>
Apr 24 '07 #3
Daz
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
Whoops. Sorry. I missed the part about there being a problem with IE.
I'm still working on this for you.

Apr 24 '07 #4
Daz
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
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.

Apr 24 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
4044
by: Mikko Ohtamaa | last post by:
From XML specification: The representation of an empty element is either a start-tag immediately followed by an end-tag, or an empty-element tag. (This means that <foo></foo> is equal to...
8
1845
by: tshad | last post by:
I have a function that formats currency to add commas and $. But I also check to see if the field is empty and if it is I return. The problem is that the field now contains the word...
4
15765
by: Manuel Faux | last post by:
Hello! I used the "File Upload - Validator" and the result is like the following: Line 13, column 204: there is no attribute "border" ....irefox!" title="Get Firefox!" border="0" /></a></p>...
28
2898
by: dingbat | last post by:
I'm writing a "tabbed folder" nav bar. Site standards are graphical prettiness, CSS throughout, valid code, but accesibility is ignored where it conflicts with prettiness. The particular issue...
3
9562
by: Paul T. Rong | last post by:
Do "" and Null both mean nothing?¡¡ If I don't type anything in text box, the its value is Null£¿¡¡Or it is ¡°¡±£¿ I don¡¯ think they are the same, but I don¡¯t know their difference. Thanks.
5
7694
by: sophie_newbie | last post by:
OK this might seem like a retarded question, but what is the difference between a library and a module? If I do: import string am I importing a module or a library? And if i do...
3
5137
by: Kceiw | last post by:
Dear all, When I use #include "queue.h", I can't link it. The error message follows: Linking... G:\Projects\Datastructure\Queue\Debug\main.o(.text+0x136): In function `main':...
8
2215
by: Cylix | last post by:
Any way to define the length of a textfield? I have just set all the field with a width so that they look like clear and easy to read, However, When I see the page of mac os, They are totally...
0
1965
by: Thomas Wittek | last post by:
Hi! I'm using xsl:attribute-sets to reduce redundancy in my XSLT. An example from a transformation to XHTML (the attribute values are simply copied from input to output): <xsl:attribute-set...
0
7220
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7105
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7371
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7023
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5617
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3188
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1534
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
410
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.