views:

698

answers:

3

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.

+4  A: 

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.

CMS
Perfect, thanks.
Chris Sobolewski
You're welcome!
CMS
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
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
A: 

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>
ninumedia
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.shtmlHowever 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
+1  A: 

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
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
@Alex, agree edited...
CMS
Yes Chris. You could either pass the actual object or the id as CMS does in his example.
Alex