If not applicable, ignore JavaScript function -
i have function changes element of markup don't have control over. has been working great, on pages - there no element of name. js compiled 1 file - , on pages no match, error: uncaught typeerror: cannot read property 'attributes' of undefined
(function($) { $.fn.changeelementtype = function(newtype) { // create object store attributes var attrs = {}; // save out values object $.each(this[0].attributes, function(idx, attr) { attrs[attr.nodename] = attr.nodevalue; }); // replace element - , put original values this.replacewith(function() { return $("<" + newtype + "/>", attrs).append($(this).contents()); }); }; })(jquery);
(for example, should list , not tons of divs) $('.list-view').changeelementtype('ul');
so instinct put if
statement or if no match there - doesn't in way. - i'm not sure on direction take.
i used use function expressions everything, think that's why i've never needed deal this.
if i'm reading question correctly, can return if there no match. happen if this.length
0
. instance change code following:
(function($) { $.fn.changeelementtype = function(newtype) { // if there no match, return if (this.length === 0) return; // create object store attributes var attrs = {}; // save out values object $.each(this[0].attributes, function(idx, attr) { attrs[attr.nodename] = attr.nodevalue; }); // replace element - , put original values this.replacewith(function() { return $("<" + newtype + "/>", attrs).append($(this).contents()); }); }; })(jquery);
Comments
Post a Comment