I have the following HTML:
<ul id="data">
<li>Value 1</li>
<li>Value 2</li>
<li>Value 3</li>
<li>Value 4</li>
</ul>
<br>
<div id="buttons">
<a id="edit">Click here to edit</a>
<br>
<a id="save">Click here to save</a>
</div>
I need to add an jQuery/Javascript snippet that transforms the <li> elements into <input> elements when the user clicks on the edit button, so he can write and change the values.
Once the process is done, with the save button the data backs to li with the values changed.
I have this jQuery code:
$('#edit').on('click',function() {
$('#data').each(
function(){
if ($(this).find('input').length){
$(this).text($(this).find('input').val());
}
else {
var t = $(this).text();
$(this).text('').append($('<input />',{'value' : t}).val(t));
}
});
});
But my idea is to separate this in two different buttons and I can't get it to work.
How can I do that? I know it shouldn't be difficult but the truth is I don't handle jQuery/Javascript very well.
Thank you in advance.
<li>elements must be contained within an<ol>or<ul>element, so your original HTML is invalid. Also, you don't need to transform theliintoinputelements, you just need to addcontenteditibleattributes to thelielements to make them editable.<li>elements contained into a<ul>. So you say that it's better to usecontenteditable="true/false"property with the buttons?