I’m finding this code extremely problematic, as it will not edit only a single list item (the item that is clicked), but will instead edit all list items at once to the same input. Any advice is appreciated. Fiddle link below:
https://jsfiddle.net/hufflepuff_hamlet/hy564btq/8/
JavaScript
x
18
18
1
<div>
2
<form name="checkListForm" class="edit-form">
3
<input type="text" name="checkListItem"/>
4
</form>
5
<div id="button1" value="button">Add!</div>
6
<div id="button2" value="button">Edit!</div>
7
8
9
<p></p>
10
<div class="list" id="item"></div>
11
12
13
<p>[Click item on list to edit item]</p>
14
<p>[Doubleclick item on list to remove item]</p>
15
16
</div>
17
</div>
18
JavaScript
1
33
33
1
//adding an item to the list
2
$(document).ready(function(){
3
$('#button1').click(function(){
4
var toAdd = $('input[name=checkListItem]').val();
5
$('.list').append("<div class='item'>" + toAdd + "</div>" + "<br>");
6
});
7
8
$('.list').click(function(){
9
});
10
11
//removing an item from the list by double-clicking on it
12
$(document).on('dblclick', '.item', function(){
13
$(this).remove();
14
});
15
16
//editing an item from the list by clicking on it and the edit button
17
$(document).on('click', '.item', function(){
18
var toAdd = $('input[name=checkListItem]').val();
19
$(this).text();
20
$(input).text(content);
21
$(this).remove();
22
$('#button2').click(function(){
23
var toAdd = $('input[name=checkListItem]').val();
24
$('.item').replaceWith("<div class='edit-form'>" + toAdd + "</div>" + "<br>");
25
26
});
27
28
$('.item').click(function(){
29
});
30
});
31
});
32
33
Advertisement
Answer
You need to use contenteditable global attribute
https://developer.mozilla.org/fr/docs/Web/HTML/Attributs_universels/contenteditable
JavaScript
1
30
30
1
$(document).ready(function(){
2
// add new item
3
$('#button1').click(function(){
4
var toAdd = $('input[name=checkListItem]').val();
5
$('.list').append("<div class='item unselectable'>" + toAdd + "</div>" + "<br>");
6
});
7
8
//removing an item from the list by dobuble clicking on it
9
$(document).on('dblclick', '.item', function(){
10
$(this).remove();
11
});
12
13
//editing an item from the list by clicking on it, editing, clicking edit button
14
$(document).on('click', '.item', function() {
15
var $currentItem = $(this);
16
var isEditable = $currentItem.is('.editable');
17
18
// check if editing input already exist
19
if (!$currentItem.is('.editable')){
20
// make content editable
21
$currentItem.prop('contenteditable',!isEditable).toggleClass('editable');
22
$currentItem.removeClass('unselectable');
23
}
24
});
25
26
// clicking button edit
27
$(document).on('click', '#button2', function() {
28
$('.item').prop('contenteditable', false).removeClass('editable').addClass('unselectable');
29
});
30
});
JavaScript
1
57
57
1
form {
2
display: inline-block;
3
}
4
5
body {
6
color: rgb(43, 61, 119);
7
font-family: garamond;
8
}
9
10
a:hover {
11
background-color: rgb(108, 86, 190);
12
}
13
14
#button1{
15
display: inline-block;
16
height:20px;
17
width:70px;
18
background-color:#5979b4;
19
font-family:arial;
20
font-weight:bold;
21
color:#ffffff;
22
border-radius: 5px;
23
text-align:center;
24
margin-top:2px;
25
}
26
27
#button2{
28
display: inline-block;
29
height:20px;
30
width:70px;
31
background-color: whitesmoke;
32
font-family:arial;
33
font-weight:bold;
34
color:#5979b4;
35
border-radius: 5px;
36
text-align:center;
37
margin-top:2px;
38
}
39
40
.list {
41
display: inline-block;
42
font-family:garamond;
43
font-weight:bold;
44
color:#3e549b;
45
}
46
.unselectable {
47
cursor:pointer;
48
-webkit-user-select: none;
49
-khtml-user-select: none;
50
-moz-user-select: none;
51
-ms-user-select: none;
52
-o-user-select: none;
53
user-select: none;
54
}
55
.editable{
56
background:#EAEAEA;
57
}
JavaScript
1
16
16
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
2
<div>
3
<form name="checkListForm" class="edit-form">
4
<input type="text" name="checkListItem"/>
5
</form>
6
<div id="button1" value="button">Add!</div>
7
<div id="button2" value="button">Edit!</div>
8
9
10
<p></p>
11
<div class="list" id="item"></div>
12
13
14
<p>[Click item on list to edit item, edit, click edit button]</p>
15
<p>[Doubleclick item on list to remove item]</p>
16
</div>