0

I have a div which gets edited on click and edited data is saved back.

Now i was to able get it focused, but somehow not able to select all text and also get the focus at end of text instead at start.

<div class="edit-section"> 
    <a>Name</a>
</div>

 <a href='#' class="edit">Edit</a>

jQuery

 $('.edit').click(function () {
        $(this).siblings('.edit-section').focus();
        $(this).siblings('.edit-section').attr('contenteditable', 'true');
        $(this).siblings('.edit-section').attr('style', 'border:2px solid;');

    });

I tried following but does not seem to work.

 $(this).siblings('.edit-section').focus(function () { $(this).select() });

Edit: New to jQuery

Richa
  • 3,261
  • 2
  • 27
  • 51

1 Answers1

2

I don’t think you can call .select() on contenteditables, but try using execCommand instead when the element is focused:

document.execCommand('selectAll', false, null);

Demo: http://jsfiddle.net/F65D3/

David Hellsing
  • 106,495
  • 44
  • 176
  • 212