简体   繁体   中英

Javascript - remove button and parent

Hi I am just learning Javascript and after following some tutorials I thought it would be nice to practise some Javascript by making stuff.

So now I am trying to make a very easy to-do-list. Just for practise, but I get stuck. I managed to add items with a remove-button to an UL with JS. But, BUT:

How do I make it so; when you click on the removeMe button, that only that Li will be removed? What should I use? Here's my code:

var buttonAdd = document.getElementById('but1');
var buttonRemove = document.getElementById('but2');

var ul = document.getElementById('myUl');

function addLi() {
var newLi = document.createElement('li');
var removeThis = document.createElement('button');
var textInput = document.getElementById('inputText').value;
if(textInput === ""){
 alert('Add text');
}else{
newLi.innerHTML = textInput;
newLi.appendChild(removeThis);
removeThis.innerHTML = "Remove me";
removeThis.setAttribute("onClick", "removeMe(this);");
ul.appendChild(newLi);
 }  
}

buttonAdd.onclick = function() {
 addLi(); 
};

buttonRemove.onclick = function() {
 ul.innerHTML = "";
};

function removeMe(item){
  //get called when clicked on the remove button
}

and my HTML:

<body>
 <ul id="myUl"></ul>
  <input id="inputText" type="text"><br />
  <button id="but1">Add stuff</button><br />
  <button id="but2">Remove all</button>
</body>

Thanks

You just need to remove the parent node (the li), as I've shown using jsbin .

function removeMe(item){
    item.parentNode.remove();
}

Please note Blue Skies's comment that this may not work across all browsers, an alternative is:

var par = item.parentNode; par.parentNode.removeChild(par);

The function remove() is a brand new DOM 4 method and not very widely supported yet. The clunky, but bulletproof way would be:

function removeMe(item){
  item.parentElement.parentElement.removeChild(item.parentElement);
}

or with a bit more elegance:

function removeMe(item){
  var parent = item.parentElement;
  parent.parentElement.removeChild(parent);
}

http://jsfiddle.net/BtbR4/

Also be careful with this:

removeThis.setAttribute("onClick", "removeMe(this);");

Handing a function reference as a string is always a bad idea for several reasons (eval'ing the string, messing up the scope). There are several better options:

removeThis.onclick = removeMe;

or if you need to hand over parameters

removeThis.onclick = function(){removeMe(your,parameters)};

The best option however is to attach eventhandlers always like this:

Element.addEventListener("type-of-event",functionReference);

a cleaner way to do things is to add

removeThis.onclick = removeMe;

and

function removeMe(mouseEvent){
    this.parentNode.remove();
}

This is consistent with how you add the other onclick functions in your code. Since you said you are learning js, it is a good idea to learn how events and functions work. So, the take away from this is that the 'this' of a function that is attached to an object is the object itself (the removeThis object in this case), and event handlers give you access to the event that invoked them (mouseEvent) in the argument list.

JSfiddle: http://jsfiddle.net/QT4E3/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM