Removing elements


I may be incorrectly adding the

element to the #two div. if I'm not, I can't get how to refer to that added element trying to remove it. thanks for any help.

error messages:

Make sure you remove your

using the .remove() function.

MY code:
   var $p = $('<p>Hello, I\'m a string</p>');


What the lesson is asking you to do is remove the paragraph, you would do it how you would with any other tag eg the div

like so:


apply the same rulings for paragraph tag


ok, so
all of these physically remove the

element from my page
$('.container p').remove();
$('container p:last').remove();
the one I had above

I appreciate your attempt to help, but I evidently cannot target the div the way the exercise wants me to do it, so can you please elaborate on your answer?


I just found the answer the exercise wanted.
I guess I was overthinking it, but don't you think it's better to be more specific?
And please point out if there is something wrong with the way I was doing it previously.
I'm very new to this (2 months thus far learning HTML, CSS, and javascript).
also target the last div of the '.container' object?

And finally
I was trying a DOM crawl

I don't think either of these last two worked, can you suggest what's wrong with my code for them to not achieve the desired result (removing the 'p' string from the page)?


you would do this:

   $('.container p').last().remove();

you will be targeting paragraph's inside .container but then adding the last will only add changes to the last paragraph.

I'm sorry I don't understand your question.
.container is the class that is specified on div

what are you trying to achieve here? explain and I can try and help you.

// will remove any paragraph off the page

//this  will remove the variable 'p'

but removing the variable the lesson won't let you pass as it wants you to remove the paragraph by targeting the paragraph tag


Thanks so much for your expanded answer!
It really helps, I didn't even realize last() didn't accept any arguments, hence why I was putting 'p' inside the parentheses.
To be honest, not entirely sure what I was trying to do with that DOM crawl. My thinking may have been: "I don't know how to refer to the 'p' I added with a specific class or id, so, perhaps I need to crawl back out from the item I can point to, and try to find the 'p' that way, not realizing that I couldn't pass an argument to the last() function.

I think that you have given enough feedback that I could target the div I want (if its last or first), but what if its the third 'p' element out of 4?
$('.container p').last().prev().remove();
work to remove the 3rd 'p'?

If that would work, what if there were 15 elements? and I wanted to remove the 7th? I mean, I can't see repeating last().prev().prev() with six more prev() being the most effective way to target like that.

Though I am too new to programming to be able to execute something like this, i could envision a loop whereby subsequent 'p' elements are created with newClass('classX') and each new 'p' receives an X value one larger than the previous. But, I can't see how I would introduce a variable into that class assignment function.

does that question make any sense?


Oh...just thinking that last question I posed through some more.

perhaps one could make an array of values that look like var listClasses = ["'class0'", "'class1'",etc.];
then loop through 'p' creation, referring to that array?

with code for the adding classes to each one created.

I am short of the skills necessary to do the actually looping creation of 'p' elements, but I think I could get there in a few days of study, when I have a chance to try. Is it worth trying to get this to work? Is there a better way than the one I envision?


you can add the same class to all those you want to target and then just target the class.

I like how you are thinking all sorts of possibilities, you editor is your playground so play around with your code.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.