Search code examples
javascripteventsprototypejslowpro

Using prototype/lowpro to reorder items on screen. JS not working


Have the following JS included in my HTML:

document.observe('dom:loaded', function() {
  Event.addBehavior( {
   'a.move-up:click':  function(event) {
        moveUp(this);
        event.stop();

    },
   'a.move-down:click':  function(event) {
        moveDown(this);
        event.stop();
    }
  });
});

function moveUp(element) {

  var questionElement = $(element).up('div.question');
  var preQuestionElement = questionElement.previous('div.question');
  moveElments('up', questionElement , preQuestionElement);
}

function moveDown(element) {
  var questionElement = $(element).up('.question');
  var postQuestionElement = questionElement.next('.question');

  moveElllments('down', questionElement , postQuestionElement);
}

function moveElments(direction, targRow, sibling) {
    var targetParent = targRow.up('div.questions');
    if(direction == 'up'){
      targRow.remove();
      targetParent.insertBefore(targRow, sibling);
    }
    if(direction == 'down'){
      sibling.remove();
      targetParent.insertBefore(sibling, targRow);
    }
}

I then have a link that, when clicked, should move a question (enclosed in div.question) up within a parent (div.questions).

<a class="move-up" href="#" style="">Move Up</a>

However it does not seem to work. Appears that the Event handler does not see the "click" event...

What is wrong with that code? Thanks!


Solution

  • Im not familiar with lowpro, but here's a solution using vanilla Prototype, and attaches your event listeners using observe:

    document.observe('dom:loaded', function() {
      $$('a.move-up').observe('click', function(event) {
        moveUp(this);
        event.stop();
      });
    
      $$('a.move-down').observe('click', function(event) {
        moveDown(this);
        event.stop();
      });
    });