tags:

views:

61

answers:

2

trying to determine a decent, cross browser method for obtaining attributes with javascript? assume javascript library use (jQuery/Mootools/etc.) is not an option.

I've tried the following, but I frequently get "attributes" is null or not an object error when IE tries to use the "else" method. Can anyone assist?

<script type="text/javascript">
//...
    getAttr: function(ele, attr) {
      if (typeof ele.attributes[attr] == 'undefined'){
        return ele.getAttribute(attr);
      } else {
        return ele.attributes[attr].nodeValue;
      }
    },
//...
</script>


<div>
 <a href="http://www.yo.com#foo"&gt;Link&lt;/a&gt;
</div>

using the above html, in each browser, how do I getAttr(ele, 'href')? (assume selecting the ele node isn't an issue)

+1  A: 

You are trying to access properties of ele before you've established if those properties exist. Try this kind of evidence chain:

if (ele.attributes && ele.attributes[attr] && typeof ele.attributes[attr] == 'undefined')

etc.

Robusto
i suppose that will get rid of my error (and thanks), but how say I used my method $.getAttr($anchor, 'href');how do I get the href from a <a href="#"> in IE6-8?
tester
Sorry, I'm not sure what you're asking.
Robusto
I updated the bottom of the question to specify what I'm trying to get at (the href of a selected link)
tester
+2  A: 

With regard to your question's update, you could try this.

It may be overkill, but if getAttribute() and the dot notation don't return a result, it iterates through the attributes object to try to find a match.

Example: http://jsfiddle.net/4ZwNs/

var funcs = {
    getAttr: function(ele, attr) {
        var result = (ele.getAttribute && ele.getAttribute(attr)) || null;
        if( !result ) {
            var attrs = ele.attributes;
            var length = attrs.length;
            for(var i = 0; i < length; i++)
                if(attr[i].nodeName === attr)
                    result = attr[i].nodeValue;
        }
        return result;
    }
};

var result = funcs.getAttr(el, 'hash');

It's up to you to do some cross-browser testing, though. :o)


Using ele.attributes, you need to access them by index, as in:

ele.attributes[0].nodeName;   // "id" (for example)
ele.attributes[0].nodeValue;  // "my_id" (for example)

Trying to pass attributes an attribute name appears to return a value whose typeof is object, so your else code is running even though ele.attributes[attr] doesn't give you the value you want.

patrick dw
I updated the question to be a little more specific. How would I specify that I want the attr "href" or "hash" using your method?
tester
@tester - I'll update in a minute.
patrick dw
works great in Chrome/FF, testing in IE8, I get:Object doesn't support this property or method on this line:var result = ele.getAttribute(attr) || ele[attr] || null;
tester
eh starting to think it's the selector I'm passing to it causing the issue
tester
patrick dw
@patrick dw, so it appears it's how I'm going about using this. What you've done works perfectly.. Having some trouble debugging ie's issue with how I'm doing this: http://jsfiddle.net/N8CxL/would appreciate any assistance you can further provide.
tester
@tester - Not sure. At first it was showing that `obj` in `bind` method was null, then it stopped. Maybe there's an issue with your DOMReady?
patrick dw
it's something up with my bind method.. something about how 'this' doesn't properly represent the current object, so I can't use your getAttr method properly..
tester
ah I figured it out.. it was the bind method.. I was going if (obj[i].addEventListener) {... instead of if (obj.addEventListener) {...
tester
Anywho, thanks for the help!! you rock!
tester
@tester - Glad you got it figured out. :o)
patrick dw
Attributes are not the same as properties. Please do not mix them up and treat them as being equivalent.
Tim Down
@Tim - You're absolutely right. Fixed.
patrick dw
@tester - Updated the answer. See comment above from @Tim Down.
patrick dw
Patrick: my comment was a bit aggressive. Sorry. The general confusion among developers over properties and attributes (largely the fault of jQuery and its confusing `attr()` method) is something of bugbear for me.
Tim Down
@Tim - It's alright. :o) I appreciate the reminder.
patrick dw