Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getElementById.value okay in IE but 'undefined' in FX

Status
Not open for further replies.

peterpann

Programmer
Jun 19, 2007
63
GB
on WinXP the alert for getElementById().value shows '500' in IE8, but 'undefined' in FX 1.5 with nothing in the JavaScript Console. What to do ? Thanks.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>rent</title>
</head>
<body>
<table>
<tr>
<td id="rent1" name="rent1" value="500">£500 pcm</td>
</tr>
</table>
<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">
<!--hide
alert(document.getElementById("rent1").value);
//stop hiding-->
</SCRIPT>
</body>
</html>
 
Hi

peterpann said:
on WinXP the alert for getElementById().value shows '500' in IE8, but 'undefined' in FX 1.5
Not just FireFox, actually all Gecko, Presto WebKit and KHTML-based browsers.

According to the standard, [tt]td[/tt] tag has no value attribute, so neither the DOM object has value property.
peterpann said:
What to do ?
Give a hint to the JavaScript engine about what you are looking for, by using the [tt]getAttribute()[/tt] method :
Code:
[COLOR=darkgoldenrod]alert[/color][teal]([/teal]document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]"rent1"[/i][/green][teal]).[/teal][COLOR=darkgoldenrod]getAttribute[/color][teal]([/teal][green][i]'value'[/i][/green][teal]));[/teal]
Thinking to the future when we will be able to use the HTML 5 [tt]data-*[/tt] attributes, I would prefix that value with data- :
Code:
[b]<td[/b] [maroon]id[/maroon][teal]=[/teal][green][i]"rent1"[/i][/green] [maroon]name[/maroon][teal]=[/teal][green][i]"rent1"[/i][/green] [maroon]data-value[/maroon][teal]=[/teal][green][i]"500"[/i][/green][b]>[/b]£500 pcm[b]</td>[/b]
Code:
[gray]// for now[/gray]
[COLOR=darkgoldenrod]alert[/color][teal]([/teal]document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]"rent1"[/i][/green][teal]).[/teal][COLOR=darkgoldenrod]getAttribute[/color][teal]([/teal][green][i]'data-value'[/i][/green][teal]));[/teal]

[gray]// in the future[/gray]
[COLOR=darkgoldenrod]alert[/color][teal]([/teal]document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]"rent1"[/i][/green][teal]).[/teal]dataset[teal][[/teal][green][i]'value'[/i][/green][teal]]);[/teal]
( See HTML5 | Semantics, structure, and APIs of HTML documents | Elements | Global attributes | Embedding custom non-visible data with the data-* attributes for more. )


Feherke.
 
Thanks Feherke, td tag has no value attribute, right, understood.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top