Loading ...
Sorry, an error occurred while loading the content.

Using array notation to create drag/drop objects

Expand Messages
  • rocketeerbkw
    I m getting dynamic information from xml and I need to create dynamic variable names from that information. The only way I thought of was using the array
    Message 1 of 4 , Apr 26, 2007
    • 0 Attachment
      I'm getting dynamic information from xml and I need to create dynamic
      variable names from that information. The only way I thought of was
      using the array notation to name them, ex
      var["obj1"], var["obj2"], var["obj3"], etc.

      This is working fine and I can set those as new YAHOO.util.DD, and
      they are draggable.

      The problem I get is when I try to reference their
      startdrag/enddrag/ondrag etc.

      Each draggable element will have the same effects that happen on each
      interesting event. So to make it easier, I am doing a for loop, like so:

      for (i=0; i<5; i++) {
      var["obj" + i].startDrag = function()
      var["obj" + i].endDrag = function()
      }

      The problem I run into is a javascript error when startDrag is called:

      var["obj" + i].starDrag is not defined


      Thank you very much for you help. I did not copy paste any code, but
      I will if I was unclear.
    • rocketeerbkw
      I thought that by giving my actual code, I would make more sense: for (i = 1; i
      Message 2 of 4 , Apr 28, 2007
      • 0 Attachment
        I thought that by giving my actual code, I would make more sense:

        for (i = 1; i <= 3; i++) {

        mnFdrg[i] = new YAHOO.util.DD("mnFdrg" + i, "Alias1", {isTarget:false});

        }

        In a seperate file I have this:
        for (i = 1; i <= 3; i++) {

        mnFdrg[i].startDrag = function (X, Y)

        {

        //ttFdrg[i].hide();

        mnFdrg[i].addToGroup("Alias2");

        mnFdrg[i].addToGroup("Alias3");

        mnFdrg[i].addToGroup("Alias4");

        mnFdrg[i].removeFromGroup("Alias1");

        getObject('status').innerHTML = this.id + " drag started";

        return true;

        }

        }

        The error I get is this:
        mnFdrg[i] has no properties
        > mnFdrg[i].addToGroup("Alias2");

        Thank you again!
      • Adam Moore
        ... Make sure you have declared i : for (var i=1; i
        Message 3 of 4 , Apr 30, 2007
        • 0 Attachment
          On Sun, Apr 29, 2007 at 12:28:14AM -0000, rocketeerbkw wrote:
          > I thought that by giving my actual code, I would make more sense:
          >
          > for (i = 1; i <= 3; i++) {
          > mnFdrg[i] = new YAHOO.util.DD("mnFdrg" + i, "Alias1", {isTarget:false});
          > }

          Make sure you have declared "i":

          for (var i=1; i<=3; i=i+1)
          mnFdrg[i] = new YAHOO.util.DD("mnFdrg" + i, "Alias1", {isTarget:false});
          }

          > In a seperate file I have this:
          > for (i = 1; i <= 3; i++) {
          > mnFdrg[i].startDrag = function (X, Y)
          > {
          > //ttFdrg[i].hide();
          > mnFdrg[i].addToGroup("Alias2");
          > mnFdrg[i].addToGroup("Alias3");
          > mnFdrg[i].addToGroup("Alias4");
          > mnFdrg[i].removeFromGroup("Alias1");
          > getObject('status').innerHTML = this.id + " drag started";
          > return true;
          > }
          > }

          Don't try to use the reference to the array item in the body of the
          startDrag function. Use "this" instead:

          for (var i=1; i<=3; i=i+1) {
          mnFdrg[i].startDrag = function (x, y) {
          this.addToGroup("Alias2");
          this.addToGroup("Alias3");
          this.addToGroup("Alias4");
          this.removeFromGroup("Alias1");
          YAHOO.util.Dom.get('status').innerHTML = this.id + " drag started";
          }
          }


          -Adam
        • rocketeerbkw
          ... started ; ... Thank you very much, that definitely helps solve that problem. In the code I posted i had //ttFdrg[i].hide(); The reason it s commented out
          Message 4 of 4 , Apr 30, 2007
          • 0 Attachment
            --- In ydn-javascript@yahoogroups.com, Adam Moore <adamoore@...> wrote:
            > Don't try to use the reference to the array item in the body of the
            > startDrag function. Use "this" instead:
            >
            > for (var i=1; i<=3; i=i+1) {
            > mnFdrg[i].startDrag = function (x, y) {
            > this.addToGroup("Alias2");
            > this.addToGroup("Alias3");
            > this.addToGroup("Alias4");
            > this.removeFromGroup("Alias1");
            > YAHOO.util.Dom.get('status').innerHTML = this.id + " drag
            started";
            > }
            > }
            >
            >
            > -Adam
            >

            Thank you very much, that definitely helps solve that problem. In the
            code I posted i had //ttFdrg[i].hide();
            The reason it's commented out was because it was giving me the same error.
            The reason I'm calling that is because it is a reference to a tooltip,
            and i want it to go away whenever someone starts dragging.
            ttFdrg[1] = new YAHOO.widget.Tooltip("ttFdrg1", {context:"mnFdrg1",
            text:"<img src=\"images/thumbnails/thumb1.gif\" />", showDelay:200});

            if I can't use "this" how can I reference that?
          Your message has been successfully submitted and would be delivered to recipients shortly.