views:

59

answers:

1

Hello,

I've just come to an unusual beghavior of Internet Explorer IE (v8.0.6001.18904). When I try to set "type" attribute of any <LI> element, it will result into error. I used jQuery (v1.32):

$('<li>').attr("type", "test");
or
$('<li type="test">');

The same thing works for DIV. LI element does not seem to have "type" attribute reserved by HTML or XHTML definitions. It also might be jQuery issue.

Solution is simple - just use another attribute name :-)

But is there someone out there who knows WHY does this error occur? Could it happen with another attribute names? Why the error comes with LI element only?

UPDATE: Quick solution for this issue:

$('<li>').data("type", "test");
+2  A: 

Instead of using attributes for this, jQuery provides another mechanism with .data(), a quick example:

$("li").data("type", "test");

$("li").click(function() {
  alert($(this).data("type")); //alerts "test"
});
Nick Craver
Yes, thank you, this perfectly solves the issue. (for jQuery) (nice for production and slightly worse for debugging - you can't see the value by quickly looking into DOM explorer :-)
Petr Urban
@Petr - Just a tip, if you're using a console of any type, you can do `$(this).data()` to dump the entire collection on that element :)
Nick Craver
thanks Nick :-) console.log($(selector).data());
Petr Urban