0

I have a ul consisting of several li's in it. Now I wanted to add Edit And Delete option infront of every li so that it can be Edited or Deleted on click.

How can i do this ?

Thanks in Advance for any help :)

Richa
  • 3,261
  • 2
  • 27
  • 51

5 Answers5

1

You can use CSS trick to perform inplace edit on span's and jQuery#remove() for deleting li's.

Idea is to hide the text when Edit is clicked and show textbox and vice-versa on click of Delete.

$('button.edit').click(function(){        
    var label_element = $(this).parent().find('span'),
        input_element = $(this).parent().find('input');
    label_element.addClass('editing');        
    input_element.val(label_element.text());
    input_element.addClass('editing');
});
$('button.delete').click(function(){        
    $(this).parent().remove();
});
 $('input').blur(function(){        
    var label_element = $(this).parent().find('span');
    label_element.text($(this).val());
    $(this).removeClass('editing');
    label_element.removeClass('editing'); 

});

Fiddle DEMO

Dhanu Gurung
  • 8,480
  • 10
  • 47
  • 60
1

I strongly suggest you use Jquery or another library to handle cross browser issues. Anyway, here is the solution with pure javascript... it should work in all modern browsers recent versions.

SOLUTION DEMO

HTML

<ul class="fa-ul">
  <li>
    <span> BE/ BTech/ MCS </span>
    <button class="remove">Delete</button>
    <button class="edit">Edit</button>
  </li>
  <li>
    <span> Solid </span>
    <button class="remove">Delete</button>
    <button class="edit">Edit</button>
  </li>
  <li>
    <span> Strong </span>
    <button class="remove">Delete</button>
    <button class="edit">Edit</button>
  </li>
  <li>
    <span> Sharp </span>
    <button class="remove">Delete</button>
    <button class="edit">Edit</button>
  </li>
  <li>
    <span> Ability</span>
    <button class="remove">Delete</button>
    <button class="edit">Edit</button>
  </li>
  <li>
    <span> Deal problems</span>
    <button class="remove">Delete</button>
    <button class="edit">Edit</button>
  </li>
  <li>
    <span> Strong </span>
    <button class="remove">Delete</button>
    <button class="edit">Edit</button>
  </li>
  <li>
    <span> Excellent </span>
    <button class="remove">Delete</button>
    <button class="edit">Edit</button>
  </li>
</ul>

JS

var removeClassElements = document.getElementsByClassName('remove');

for(var i = 0; i < removeClassElements.length; i++){
  var element = removeClassElements[i];  
  element.addEventListener('click', remove);
}

var editClassElements = document.getElementsByClassName('edit');

for(var i = 0; i < editClassElements.length; i++){
  var element = editClassElements[i];  
  element.addEventListener('click', edit);
}

function remove() {
  var li = this.parentNode;
  var ul = li.parentNode;  
  ul.removeChild(li);
}

function edit() {
  var li = this.parentNode;
  var span = li.children[0];
  span.setAttribute('contenteditable', true);
  span.focus();
}

PS: You don't need class="lead justified" in each li element. You can use a CSS rule like this:

.fa-ul li span:first-child {...}
rafaelcastrocouto
  • 11,781
  • 3
  • 38
  • 63
  • It is working perfectly in JS fiddle, but when i implement it in my code it says removeClassElements is undefined. Thanks – Richa Mar 25 '14 at 13:08
  • Are you running all the code in the same context? Do you have separate files? Are you using frames or iframes? – rafaelcastrocouto Mar 25 '14 at 13:10
  • I am running all the code in same context , no separate files.And i am not using frames or iframes. – Richa Mar 25 '14 at 13:14
  • Are you sure you don't have a typo error? If so, try debuging using `console.log(removeClassElements);` after the variable declaration. It should display the elements in the dev console (F12). – rafaelcastrocouto Mar 25 '14 at 13:17
  • Created JS fiddle and this is how it is working http://jsfiddle.net/srahil33/wN23b/ – Richa Mar 26 '14 at 05:01
  • I changed my mind and wanted a complete div to get editable on single click instead of editing single single list item, and i got it working using jquery. Would you help me one more thing?? – Richa Mar 26 '14 at 11:48
  • sure ... but if it's a different subject you should create another question. – rafaelcastrocouto Mar 26 '14 at 11:54
  • I have created another question, will you have a look at it please http://stackoverflow.com/questions/22657009/edit-div-and-have-updated-information-on-page-reload – Richa Mar 26 '14 at 11:56
  • Please help sir, i am not getting how to do it – Richa Mar 26 '14 at 12:04
  • take a look here: http://gordoncluster.wordpress.com/2013/09/18/how-to-insert-data-into-mysql-database-using-jquery-ajax-php/ – rafaelcastrocouto Mar 26 '14 at 12:43
1

See this DEMO

$('ul li').each(function () {
    $(this).append('<a class="delete" href="#">Delete</a>  <a href="#" class="edit">Edit</a>')
});
$('ul li a.delete').on('click', function () {
    $(this).parent().remove();
    return false;
});
$('ul li a.edit').on('click', function () {
    var val = $(this).siblings('span').html();
    if (val) {
        $(this).parent().prepend('<input type="text" class="txt" value="' + val + '" />');
        $(this).siblings('span').remove();
        $(this).html('Update');
    } else {
        var $txt = $(this).siblings().filter(function() { return $(this).hasClass('txt') });
        $(this).parent().prepend('<span class="lead justified">' + $txt.val() + '</span>');
        $txt.remove();
        $(this).html('Edit');
    }
    return false;
});
Anujith
  • 9,370
  • 6
  • 33
  • 48
  • Well it is appending but not to given li,i tried specifying ul class name in first line but still not working :( – Richa Mar 25 '14 at 13:16
  • @RS26 can you make it clear what is not working? Did you checked the fiddle sample..? – Anujith Mar 25 '14 at 13:21
  • Yes i did, and it is working fine in fiddle. But when i implement it in my code, delete and edit option are placed in Navigation Bar instead of being placed infront of li – Richa Mar 25 '14 at 13:24
0
<ul class="fa-ul">
  <li><span class="lead justified"> BE/ BTech/ MCS </span><a href="#" onclick="remove()">Delete</a><a href="#" onclick="edit(this)">Edit</a></li>...

The function edit(this) is given incorrectly and this links to <a>Edit</a>. You should add id property to the <span>.

<li><span class="lead justified" id="foo"> BE/ BTech/ MCS </span><a href="#" onclick="remove()">Delete</a><a href="#" onclick="edit(foo)">Edit</a></li>

<script>
function edit(target) {
     target.contenteditable = true;
}
</script>
GRIM2D
  • 11
  • 2
0

You need do change some changes in your markups, You can take <div> instead of <span> and can apply HTML5's attribute contenteditable

Sample Markup

<li>
    <div class="lead justified">BE/ BTech/ MCS</div>
    <a href="#" onclick="remove(this)">Delete</a>
    <a href="#" onclick="edit(this)">Edit</a>
</li>

Script

Edit

function edit(elem){
    $(elem).siblings('div.lead').attr('contenteditable',true);
}

Delete

function remove(elem){   
    $(elem).closest('li').remove(); // Deletes li
    return false;
}

Fiddle Demo

Dhaval Marthak
  • 17,246
  • 6
  • 46
  • 68