V
V
Valen962018-02-10 16:57:17
JavaScript
Valen96, 2018-02-10 16:57:17

How to remove the last paragraph on a page?

I want to make a button that deletes the last paragraph on the page,
the button has an event handler: onclick="delLastParagraph();".
I'm trying to write a function like this:

function delLastParagraph()
  {
  	var paragraphs = document.querySelectorAll('p');

It selects all the paragraphs in the NodeList, displays everything correctly in the console, all paragraph nodes on the page, and then how to delete the last one from the DOM tree by clicking the button?

var lastParagparh=paragraphs.lastChild;
So it doesn't work, to find the last one, you need to do it through the array?
And how is the removal? Through remove()? Or removeChild?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
entermix, 2018-02-10
@Valen96

Even though you were too lazy to even frame your question: https://api.jquery.com/last/ https://api.jquery.com/last-selector/ https://api.jquery.com/remove/

var nodes = div.querySelectorAll('p');
var first = nodes[0];
var last = nodes[nodes.length- 1];

last.remove();

https://stackoverflow.com/questions/5684811/in-que...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question