0

I have a page where I add divs dynamically. All Divs are draggable and resizable. If I click on the div, it should become editable. Here is my test code.

http://jsfiddle.net/hqhqb5ut/1/

Click on the first Div to see this.

But If I click to drag the div, only dragging should happen. This works fine if there is only one div. But If I add more divs, only the last one behaves correctly. The previous ones become editable, even if I drag.

I checked the console, and it seems like the function is being called multiple times because there are more than one div with same class.

How can I solve this?

UPDATE 1: I have the dynamic part figured out. I saw the SO question, that was suggested as duplicate. But here is a specific problem. If you click on the "Add div", and try to drag the divs, only the LAST ADDED DIV can be dragged. offcourse, there is a function to check if it is being dragged, but that fails due to multiple divs.

http://jsfiddle.net/hqhqb5ut/1/

aVC
  • 2,056
  • 2
  • 19
  • 44
  • @arun-p-johny I saw the question, but that is not solving the issue. Can you take a look at the updated fiddle? and see if you can drag the divs after you add more divs? only the last added div can be dragged. – aVC Nov 03 '14 at 03:39
  • I re-opened your question, but please make sure to put here all the required info. Don't just link to it. Thanks and good luck. – gdoron is supporting Monica Nov 03 '14 at 03:44
  • 1
    @aVC I'm able to drag all the added div's.... can you check it again – Arun P Johny Nov 03 '14 at 03:45
  • @ArunPJohny It is draggable the first time. But if you click inside a div once, and click outside, then it wont be draggable. – aVC Nov 03 '14 at 03:49

1 Answers1

0

Use event delegation and your code should work fine:

$('#documentMain').on('mousedown', '.dv1', function() { ..... });

Demo

As you can see in the demo, the change makes the added divs both editable and draggable multiple times.

PeterKA
  • 21,234
  • 4
  • 21
  • 44
  • not for me. If you add a div, click on the

    inside this newly added Div, Click outside again, and then try to drag this Div

    – aVC Nov 03 '14 at 03:52
  • What browser are you on? I am on Chrome and everything works great! – PeterKA Nov 03 '14 at 03:53
  • Chromium on ubuntu14. The first div is good. I agree. Can you repeat the effect for a newly added div? – aVC Nov 03 '14 at 03:54
  • Quite likely you're trying to drug it via the editable area; you've got to grab on non-editable area. The cursor has to be a pointer. – PeterKA Nov 03 '14 at 03:55
  • Exactly. Which is why I have a function validateClick() that checks this to see if I am dragging, or clicking to edit. :( – aVC Nov 03 '14 at 03:56
  • Checking on chrome on a mac, same issue. – aVC Nov 03 '14 at 03:57
  • I think I know why this is happening. You may not be keeping good track of dragging. One sec. – PeterKA Nov 03 '14 at 03:59
  • Sure. Appreciate it. I noted that the first original div has no issues. only the newly added ones show the trouble. – aVC Nov 03 '14 at 04:04
  • Can't seem to catch it now. – PeterKA Nov 03 '14 at 04:15
  • Really? It still shows the same issue to me. I am looking at the demo fiddle you posted. http://jsfiddle.net/hqhqb5ut/2/ – aVC Nov 03 '14 at 04:17
  • I just noticed that in the new divs, the Textareas are not being reset to

    on blur. I made few quick changes, http://jsfiddle.net/hqhqb5ut/3/ this seems to do the job. I am not sure if I am missing anything

    – aVC Nov 03 '14 at 04:26
  • Yap, glad you nailed :) Looks great! Sorry I could not help much. – PeterKA Nov 03 '14 at 04:38