0

I have a text field which can be edited using click and i am doing it in jQuery.

Now i was wondering how can i select all the text in box when i click EDIT button

Fiddle Link

Code for edit

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

I cannot add separate click function for .edit-section

Richa
  • 3,261
  • 2
  • 27
  • 51

4 Answers4

3

Try,

 $('.edit,.done').click(function () {
     var section = $(this).siblings('.edit-section');
     var isEdit = $(this).is('.edit');
     section.prop('contenteditable',isEdit).css('border',isEdit?"2px solid":"none");
     $('.edit').toggle(!isEdit);
     $('.done').toggle(isEdit);
     if(isEdit){section.focus(); document.execCommand('selectAll',false,null); }
 });

DEMO


Source

Community
  • 1
  • 1
Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
3

Demo

jQuery.fn.selectText = function () {
    var doc = document;
    var element = this[0];
    console.log(this, element);
    if (doc.body.createTextRange) {
        var range = document.body.createTextRange();
        range.moveToElementText(element);
        range.select();
    } else if (window.getSelection) {
        var selection = window.getSelection();
        var range = document.createRange();
        range.selectNodeContents(element);
        selection.removeAllRanges();
        selection.addRange(range);
    }
};

$(".edit").click(function () {
    $(".edit-section").selectText();
});

from : Selecting text in an element (akin to highlighting with your mouse)

Community
  • 1
  • 1
4dgaurav
  • 11,360
  • 4
  • 32
  • 59
2

Possible duplicate - Programmatically select text in a contenteditable HTML element?

Copied following function from the answer.

function selectElementContents(el) {
    var range = document.createRange();
    range.selectNodeContents(el);
    var sel = window.getSelection();
    sel.removeAllRanges();
    sel.addRange(range);
}

var el = document.getElementById("foo");
selectElementContents(el);

Updated JS-Fiddle - http://jsfiddle.net/vishwanatharondekar/zSwBL/5/

Community
  • 1
  • 1
Vishwanath
  • 6,284
  • 4
  • 38
  • 57
1

Try

jQuery.fn.selectText = function(){
    var doc = document;
    var element = this[0];
    console.log(this, element);
    if (doc.body.createTextRange) {
        var range = document.body.createTextRange();
        range.moveToElementText(element);
        range.select();
    } else if (window.getSelection) {
        var selection = window.getSelection();        
        var range = document.createRange();
        range.selectNodeContents(element);
        selection.removeAllRanges();
        selection.addRange(range);
    }
};

$("button").click(function() {
    $("#editable").selectText();
});

JSFiddle

Refence: How to select all text in contenteditable div?

Community
  • 1
  • 1
Okky
  • 10,338
  • 15
  • 75
  • 122