8

Is this possible?

I am attempting to write a function for onmousedown that will return the ID of the element you just clicked for later use in recreating that element in a different div.

zombat
  • 92,731
  • 24
  • 156
  • 164
Chris Sobolewski
  • 12,819
  • 12
  • 63
  • 96

4 Answers4

16

You can use event delegation, to basically connect only one event handler to your entire document, and get the element which the event was originally dispatched, using event.target:

document.body.onmousedown = function (e) {
  e = e || window.event;
  var elementId = (e.target || e.srcElement).id;

  // call your re-create function
  recreate(elementId);
  // ...
}

function recreate (id) {
  // you can do the DOM manipulation here.
}

Edit: You can assign events to all your Scriptaculous draggables in this way:

Event.observe(window, 'load', function () {
  Draggables.drags.each(function (item) {
    Event.observe(item.element, 'mousedown', function () {
      alert('mouseDown ' + this.id); // the this variable is the element 
    });                              // which has been "mouse downed"
  });
});

Check an example here.

Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • 1
    It's worth noting the higher up in the DOM you handle an event like this, the more careful you need to be about what you do inside the handler, as it may potentially get fired *a lot*. – Rex M Aug 09 '09 at 04:25
  • You know, upon further coding, this does not seem to be working when i click on a scriptaculous draggable. The event doesn't fire as it does when i click on a static element, so no ID is returned and my item replication fails. =\ – Chris Sobolewski Aug 10 '09 at 16:10
2

CMS pretty much has the correct answer but you will need to make it a little more cross browser friendly.

document.body.onmousedown = function (e) {
  // Get IE event object
  e = e || window.event;
  // Get target in W3C browsers & IE
  var elementId = e.target ? e.target.id : e.srcElement.id;
  // ...
}
Alex
  • 34,776
  • 10
  • 53
  • 68
  • To make sure this is clear, e would then have the value of my element which I could then pass to a global variable, and/or call a new function to pass in e as the parameter, yes? – Chris Sobolewski Aug 09 '09 at 04:15
  • Yes Chris. You could either pass the actual object or the id as CMS does in his example. – Alex Aug 09 '09 at 10:19
0

Pls insert this code to your javascript.

document.getElementById("article").onmouseup(handMu);
美丽美丽花
  • 212
  • 1
  • 3
  • 16
-1

If you want to replicate the div id, an easy way might be cloneNode like this:

<div id="node1">
  <span>ChildNode</span>
  <span>ChildNode</span>
</div>

<div id="container"></div>

<script type="text/javascript">
  var node1 = document.getElementById('node1');
  var node2 = node1.cloneNode(true);

  node2.setAttribute('id', 'node2');

  var container = document.getElementById('container');
  container.appendChild(node2);
</script>
ninu
  • 890
  • 2
  • 10
  • 26
  • The original needs to be deleted as well. I am trying to tack on some code to the Scriptaculous drag/drop functionality which will delete the original, then write a new one in the droppable div which must have the same class and ID. I'm not going to lie, I'm not a genious with javascript quite yet, I found my inspiration here: http://ashishware.com/MochikitDnD.shtml However he is assigning an id of Math.random(), whereas I need to retain the same ID, so I am trying to patch this in, as well as make it work with an unknown number of draggables. I believe I have the second part down, though. – Chris Sobolewski Aug 09 '09 at 04:11