The Little Guy Posted January 30, 2011 Share Posted January 30, 2011 I have this: JSLive.extend = function(destination, source) { for (var property in source) { if (source.hasOwnProperty(property)) { destination[property] = source[property]; } } return destination; } JSLive.prototype.item = function(itm){ if(JSLive.is_string(itm)){ if(itm.match(/^#/)){ return document.getElementById(itm.replace(/^#/, '')); }else{ if(this.getElementsByTagName){ return this.getElementsByTagName(itm); }else{ return document.getElementsByTagName(itm); } } }else{ return itm; } } JSLive.prototype.att = function(attribute, setting){ alert(this); } I want to use it like this: <p> <a href="http://google.com" id="link">Google.com</a> </p> URL: <input type="text" id="url" /><br /> <input type="button" value="Change" onclick="live.item('#link').att('href', 'http://yahoo.com')" /> basically what that does is take the link, and modify the href attribute, but I can not get it to work, I am getting the following error: Uncaught TypeError: Object http://google.com/ has no method 'att' Any suggestions on what I am doing wrong? Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted January 30, 2011 Author Share Posted January 30, 2011 Here is a solution I found: HTMLElement.prototype.attr = function(attribute, setting){ if(setting == undefined) return this.getAttribute(attribute); else{ this.setAttribute(attribute, setting); return true; } } it works, an suggestions? Quote Link to comment Share on other sites More sharing options...
trq Posted January 31, 2011 Share Posted January 31, 2011 Chaining methods is basically the same in any language. Each method needs to return in instance of the same object it is defined within. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.