Actually, to better pose my question, how can I create an object that contains an HTML element with an event listener that adds a new instance of the same object to the page (and with an incremented id)?
Should I be adding the event listener from outside the object entirely? Or am I close but not quite right with my first efforts? On Apr 28, 12:53 pm, Jangla <jangla.m...@gmail.com> wrote: > Here's some castly cimplified code of what I'm trying to do: > > var myClass = Class.create({ > initialize : function() { > this.id = 0; > this.type = 'Undefined'; > this.assignedAudienceTypes = []; > this.title = 'Untitled'; > this.add(); > return this; > }, > add : function(renderTo) { > this.setId(); > var containerDiv = document.createElement('div'); > containerDiv.writeAttribute('class', 'element-container'); > renderTo.appendChild(containerDiv); > containerDiv.writeAttribute('id', 'element-container-' + this.id); > > var buttonDiv = document.createElement('div'); > buttonDiv.writeAttribute('class', 'button-container'); > renderTo.appendChild(buttonDiv); > buttonDiv.writeAttribute('id', 'button-' + this.id); > > Event.observe('button-' + this.id, 'click', this.add); > }, > setId : function(){ > if ( $$('.element-container').length ) { > this.id = $$('.element-container').length - 1; > } > } > > }); > > The basic idea is that I'm creating some html elements on the page > which contain a button which will add another new one to the page. > > My code above is clearly wrong (I'm just setting out with full on > prototype coding) but I'm struggling to understand how to correct it. > For a start, while the setId function works first time round, second > time around "this" doesn't refer to the object but to the button > element that was clicked. > > Any pointers greatly appreciated :) > > -- > You received this message because you are subscribed to the Google Groups > "Prototype & script.aculo.us" group. > To post to this group, send email to prototype-scriptacul...@googlegroups.com. > To unsubscribe from this group, send email to > prototype-scriptaculous+unsubscr...@googlegroups.com. > For more options, visit this group > athttp://groups.google.com/group/prototype-scriptaculous?hl=en. -- You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptacul...@googlegroups.com. To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en.