javascript - Text editing with jquery -
i creating simple product list. want implement editing of product. when double-click on product, able edit it. dont understand how can this. hope, understand me because english not primary language. code here:
<div class="container"> <h1>product list</h1> <input type="text" name="newproduct" id="newproduct" placeholder="enter product here"/> <ul id="productlist"></ul> <input type="checkbox" id="selectall"/><label for="select all">select all</label> <button id="deletedoneproducts">delete selected</button> </div>
css
* { box-sizing:border-box; } body { font-family: tahoma, sans-serif; } .container { margin:0 auto; width:600px; } h1, #newproduct { text-align: center; width:598px; } #newproduct { border:1px solid #999; padding: 20px; font-size: 28px; box-shadow: 0px 0px 3px #888; } #productlist { list-style: none; padding-left:0; background-color: #f2f2f2; } .product { padding: 15px 0px 15px 40px; margin: 10px; position: relative; font-size: 24px; box-shadow: 2px 2px 3px #848484; background-color: #fff; cursor: pointer; text-transform: capitalize; color:#000; overflow: hidden; } .product:hover { background-color: #f2f2f2; } .doneproduct { margin-right: 40px; } .remove { background-image: url(ico/delete_ico.png); background-position: 0 0; width:30px; height: 30px; position: absolute; right: 20px; top:13px; display: none; } .remove:hover { background-position: -34px 0px; } .product:hover .remove { display: block; } #deletedoneproducts { float: right; background-color: #a3d5df; color: #fff; padding: 10px; border: none; text-transform: uppercase; font-weight: 900; } #deletedoneproducts:hover { background-color: #5fb5c7; cursor: pointer; }
jquery
function addnewproduct(e) { if(e.keycode == 13) { var toadd = $('input[name=newproduct]').val(); $('#productlist').append('<li class="product"> <input type="checkbox" class="doneproduct"/>'+toadd+'<div class="remove"></div><li/>'); $('#newproduct').val(''); e.preventdefault(); } }; function deleteproduct() { $(this).parent().remove(); }; function productdone() { if (!$(this).is(":checked")){ $(this).parent().css({'textdecoration': 'none', 'color': '#000'}) } else { $(this).parent().css({'textdecoration': 'line-through', 'color': '#999'}); }; }; function deleteallselected() { $(".doneproduct:checked").parent().remove(); $('input[type="checkbox"]').removeattr("checked"); }; function selectallproducts() { if (!$(".doneproduct").is(":checked")) { $(".doneproduct").prop('checked', this.checked); $(".doneproduct").parent().css({'textdecoration': 'line-through', 'color': '#999'}); } else { $(".doneproduct").parent().css({'textdecoration': 'none', 'color': '#000'}); $(".doneproduct").prop('checked', this.checked); } }; $(function() { $("#newproduct").on('keypress', addnewproduct); $(document).on('click', ".remove", deleteproduct); $(document).on('change', ".doneproduct", productdone); $("#deletedoneproducts").on('click', deleteallselected); $("#selectall").on('click', selectallproducts); $(".product").on('dbclick', editproductname); })
https://jsfiddle.net/qp3nnfc5/5/ - fiddle)
try fiddle:
change addnewproduct function to:
function addnewproduct(e) { if(e.keycode == 13) { var toadd = $('input[name=newproduct]').val(); $('#productlist').append('<li class="product"> <input type="checkbox" class="doneproduct"/><span>'+toadd+'</span><div class="remove"></div><li/>'); $('#newproduct').val(''); e.preventdefault(); }; };
and add js:
$("#productlist").on("dblclick","li",function(){ $(this).find('span').attr("contenteditable",true) })
update :
updated fiddle
add js
$("#productlist").on("keypress","li",function(e){ if(e.which == 13){ e.preventdefault() $(this).find('span').attr("contenteditable",false) return; } })
Comments
Post a Comment