views:

118

answers:

4

What changes do I have to make to the following simple HTML page to get Firefox to read and set a custom attribute as it does in IE?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Original Value</title>
</head>

<body>

<div MyAttribute="Original Value"  id="Label1">Hello World</div>

<form method="post">
    <table style="width: 100%">
     <tr>
      <td>
      <input name="Button1" onclick="button1_click();" type="button" value="button" /></td>
     </tr>
    </table>
    <script type="text/javascript">
function button1_click(){

alert("Enter");
//alert("Label1.MyAttribute " + Label1.MyAttribute);
alert(Label1.getAttribute("MyAttribute"));

Label1.MyAttribute = "Updated";
alert("Label1.MyAttribute " + Label1.MyAttribute);

}
</script>
</form>

</body>

</html>
A: 

Try:

function button1_click(){
    alert("Enter");

    var label = document.getElementById('Label1');
    alert(label.getAttribute("MyAttribute"));
    label.MyAttribute = "Updated";
    alert("Label1.MyAttribute " + label.MyAttribute);

}

IE seems to search element IDs (and probably names) when you use an undefined global variable in your JavaScript. Firefox does not.

You could argue that either behavior is correct, but I would have to side with Firefox in this one. I don't like browsers assuming things about my code. Only makes minor errors, like the once in your original code, harder to spot.

Atli
Why the down-vote?... Just out of curiosity :]
Atli
A: 

Your markup would be invalid. I've seen people do this sort of thing before but have never tried to understand it for that reason. How can you force any application to understand the it unless you used a different mime-type? And what about other browsers?

Rob
The great semantic web debate. :( How would you deliver custom attributes right now?
AnthonyWJones
I think(?) we are probably in agreement that customer attributes, like winform control tags, are useful, if this feature, intended or not, works in all major browsers, why not use it? Concern that because it is not part of an official spec that it may stop working? :-)
Velika
data-*="". See http://www.whatwg.org/html5/#embedding-custom-non-visible-data
Ms2ger
+3  A: 

You need to assign node instance to the Label1 variable:

var Label1 = document.getElementById("Label1");

Also, when working with DOM, do not set values to properties, better do this with, setAttribute function

nemisj
+5  A: 

You've discovered getAttribute but you need to retrieve the element with document.getElemenById and you need to use setAttribute to modify the attribute.

function button1_click()
{
  alert("Enter");
  var label1 = document.getElementById("Label1"); 
  alert(label1.getAttribute("MyAttribute"));
  label1.setAttribute("MyAttribute", "Updated");
  alert("Label1.MyAttribute " + label1.getAttribute("MyAttribute"));
}
AnthonyWJones