How to make an element's content editable with JavaScript (or jQuery)

62,107

Solution 1

Solution to toggle editable on/off using a click handler:

$('button').click(function(){
    var $div=$('div'), isEditable=$div.is('.editable');
    $div.prop('contenteditable',!isEditable).toggleClass('editable')
})

DEMO

Solution 2

JavaScript:

document.getElementById('elementId').contentEditable = 'true';

Solution 3

Look at this example i've made: http://jsfiddle.net/K6W7J/

JS:

$('button').click(function(){
    $('div').attr('contenteditable','true');
})

HTML:

<button>CLICK</button>
<div>YOUR CONTENT</div>
Share:
62,107

Related videos on Youtube

Damjan Pavlica
Author by

Damjan Pavlica

Software Developer and Wiki author from Belgrade. The founder of skolakoda.org. I am here to learn.

Updated on June 03, 2020

Comments

  • Damjan Pavlica
    Damjan Pavlica almost 4 years

    How would I make it so the content of an element could be made editable through native JavaScript (DOM) and/or jQuery?

  • szeitlin
    szeitlin almost 9 years
    original link is still there, but all the links therein are now defunct. :(
  • charlietfl
    charlietfl over 8 years
    @Jamen it's really not hard to convert it. try it and if you have problems create another question