Search code examples
javascriptjqueryhtmlcontenteditable

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


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


Solution

  • 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