Edit the text of a div and its children when clicking

1

How to edit the text of a div or of your children by clicking on the text with JavaScript or jQuery ?

Example, by double-clicking Text Here you can change the text and / or delete it

#alterar {background:#e3e3e3;padding:15px;}
#naoAlterar {background:#ccc;padding:5px;margin:-5px}
<div id="alterar">
Texto aqui
<p>Texto aqui</p>
<span>Texto aqui</span>
<br/><br/>
<div id="naoAlterar">Texto aqui</div>
</div>
    
asked by anonymous 21.08.2018 / 03:13

1 answer

1

You can add to the contentEditable="true" div to make the element editable: developer.mozilla .org

As you just want the double click, you can do this using an event:

$('#banner-message').dblclick(function() {
   $(this).attr("contentEditable","true");
});

$('#banner-message').blur(function() {
   $(this).attr("contentEditable","false");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divid="banner-message" >
  <p>Hello World</p>
</div>
    
21.08.2018 / 05:23