I'm trying to replace html tags to input with javascript If the tag is pressed it turns to input
Example:
<a href="#">text</a>
replace to:
<input value="text" />
or:
<h2 href="#">text</h2>
replace to:
<input value="text" />
This is the code :
/**
We're defining the event on the `body` element,
because we know the `body` is not going away.
Second argument makes sure the callback only fires when
the `click` event happens only on elements marked as `data-editable`
*/
$('body').on('click', '[data-editable]', function(){
var $el = $(this);
var $input = $('<input/>').val( $el.text() );
$el.replaceWith( $input );
var save = function(){
var $p = $('<p data-editable />').text( $input.val() );
$input.replaceWith( $p );
};
/**
We're defining the callback with `one`, because we know that
the element will be gone just after that, and we don't want
any callbacks leftovers take memory.
Next time `p` turns into `input` this single callback
will be applied again.
*/
$input.one('blur', save).focus();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p data-editable>First Element</p>
<a data-editable class="navbar-brand" href="#page-top">Second Element</a>
<p>Not editable</p>
The problem is: my classes deleted and also not be a tag that has been created 'p' and not "a" or "h2" or whatever
detach()the element from the DOM intact, and put theinputin its place. Then re-attach it afterwards, simply changing the text. That way, all the classes, events etc etc of the element should be preserved. api.jquery.com/detach