[Javascript] Wrestling with DOM access (artificial attributes, IE vs Firefox vs Safari)
Roger Roelofs
rer at datacompusa.com
Sat Aug 6 08:09:36 CDT 2005
Charls,
On Aug 6, 2005, at 4:33 AM, Charles Albrecht wrote:
> An example of what I'm working with below is at:
> http://euonymic.com/~charlesa/test/menu_test.html
>
> I'm trying to find a more browser-neutral way to access artificial
> attributes I've attached to blocks in my HTML than...
>
> var node = document.getElementById("name");
> oldvalue = node.artificialattribute // IE, Safari
> oldvalue = node.getAttributeNode("artificialattribute").value //
> Firefox, Safari
>
> var newvalue = oldvalue;
> node.artificialattribute = value; // IE, Safari
> node.setAttribute("artificialattribute", value); // Firefox,
> Safari
>
> In my case, the element I'm looking at is something like:
>
> <tr id="row512" level="2" hidecount="0" expanded="true"> [1]
>
> Now, I'm not particularly attached to leaving these in the HTML.
> (hidecount and expanded merely store state and start with default
> values anyway. level's more difficult, because I still need it in the
> generated source somewhere.) Actually, I'd probably prefer to get them
> out of the tag attributes so the page will come closer to validating
> against its doctype.
A sample page would help us give more 'context-sensitive' help, but I
often abuse the class attribute for this kind of stuff. The class
attribute is a space delimited _list_ of values, so you could have
something like
<tr id="row512" class="level2 hidecount0 expanded">
var rowEl = document.getElementById("row512");
var aState = rowEl.className.split(" ");
...
One advantage of this approach is that I almost never write
rowEl.style.display = "none"; because that stuff all ends up in the
css with class selectors.
hth
Roger,
Roger Roelofs
Know what you value.
More information about the Javascript
mailing list