Javascript: click and dblclick deliver different parentNodes on same html. Why?

Posted by user1658206 on Stack Overflow See other posts from Stack Overflow or by user1658206
Published on 2012-09-09T15:36:38Z Indexed on 2012/09/09 15:37 UTC
Read the original article Hit count: 187

Filed under:
|
|
|

currently I work on an very small wysiwyg editor based on jquery. I dont care about IE oder Chrome, just Firefox. My problem is to find if the selection is in a link for to get the value of the href attribute if is set. With click the node of the link is found, with double click always the body. It is in designMode.

My event-handler for click and dblclick. The vars current_selection, current_node, iframe and container are global.

selection_handler:function()
{
  current_selection = iframe.getSelection();
  current_node = current_selection.anchorNode;
  if(current_node.nodeName == "#text")
  {
    current_node = current_node.parentNode;
  }
  $('#log').text(current_node.nodeName);
},

The log shows mit for example 'body', when I click in unformated text. When I add a link with execCommand('createLink',...) the log shows 'A'. That works. When I mark the linked word with 2 click from start to end, the log shows 'A'. But with double click I always get 'body'. So I can't get the href attribute.

The handler is defined in the init:

init:function(options)
{
  ...

  iframe = $('#wysiwyg-'+container.attr('id'))[0].contentWindow;
  iframe.addEventListener('dblclick',methods.selection_handler,false);
  iframe.addEventListener('click',methods.selection_handler,false);

  ...
}

Has somebody an idea what is wrong?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about click