Dreamer
Dreamer

Reputation: 7549

dojo.getAttr is not a function

I am writing a dojo function with Dojo 1.6 that can modify attribute value:

    function replaceAttributeDojo(obj, attrName, newValue) {
        var value = dojo.getAttr(obj, attrName);
        if (value !== 'undefined') {
            //console.log('Found attribute '+attrName+' on object '+obj.attr('nodeName')+'[id='+obj.attr('id')+', name='+obj.attr('name')+', widgetid='+obj.attr('widgetid')+']');
            if (value == '') {
                //console.log('Attribute value is empty, removing the attribute');
                //obj.removeAttr(attrName);
                return;
            }
            var newAttrValue = value.replace(/[\d]+/g, newValue);
            dojo.setAttr(obj, attrName, newAttrValue);
        } else {
            //console.log('Did not find attribute '+attrName+' on object '+obj.attr('nodeName')+'[id='+obj.attr('id')+', name='+obj.attr('name')+', widgetid='+obj.attr('widgetid')+']');
        }
    }

Trace the code and find firebug lost on this line

var value = dojo.getAttr(obj, attrName);

And it telling me

dojo.getAttr is not a function

The function is defined outside dojo.ready, but is called inside of dojo.ready().

Here is the section call the function inside dojo.ready()

dojo.query("div, input, select", row).forEach(function(){
    replaceAttributeDojo(row, 'id' , index);
    replaceAttributeDojo(row, 'name' , index);
    replaceAttributeDojo(row, 'widgetid' , index);
});

Everything on the same page inside dojo.ready() works fine, so how could this possible?

Upvotes: 0

Views: 739

Answers (1)

Craig Swing
Craig Swing

Reputation: 8162

If obj is a DOM node:

var val = dojo.attr(node, attrName); // getter

dojo.attr(node, attrName, newValue); // setter

http://dojotoolkit.org/reference-guide/1.6/dojo/attr.html

If obj is a Widget:

var val = widget.get(attrName); // getter

widget.set(attrName, newValue); // setter

http://dojotoolkit.org/reference-guide/1.8/dijit/_WidgetBase.html

Upvotes: 2

Related Questions