1

I am binding a click event to generate a content-editable textbox. Following is the code which I use to achieve that

$('.clickable-div').bind('click', function(ev) {                                                
                                    var $div = $(ev.target);
                                    var offset = $div.offset();
                                    var x = ev.clientX - offset.left;
                                    var y = ev.clientY - offset.top;

                                    $("#main-div")
                                            .append(
                                    '<div class="handle bar"  style="top:'+y+'px;left:'+x+'px">'); 
                                                             });

The problem is when I click on the page, the textbox appears as expected but the mouse position moves abruptly to another location on the page. Also this unexpected behaviour occurs only under following two conditions together:

1) When something is written in the text box and then new click is made to generate another text box.

2) When page is scrolled and the old textbox is no more visible.

Below are the snapshots of the ideal and the faulty cases

Ideal ClickFaulty Click

Any help will be appreciated.

Rakhi
  • 135
  • 9
  • you should add position absolute to the appended div (handle bar) otherwise it will shift your stuff – Pete Jun 13 '14 at 09:54
  • @Pete, The textbox is being created at the desired location(where it is clicked). The problem is that the mouse pointer shifts after the click by some distance. Also I have already added position:absolute to the appended div – Rakhi Jun 13 '14 at 10:00
  • are right I didn't think I understood your question properly, what if you add `$(this).focus()` at the end of your click event – Pete Jun 13 '14 at 10:32
  • I am just clarifying my question - The mouse pointer moves automatically to an altogether new position(ideally is should be there where it is clicked), which is the problem. However the textbox is generated at the right place – Rakhi Jun 13 '14 at 10:53

0 Answers0