5. Removing elements. Removed from HTML viewer but still get error


#1



https://www.codecademy.com/courses/web-beginner-en-v6phg/0/5?curriculum_id=50a3fad8c7a770b5fd0007a1#


Get this: Oops, try again.
Make sure you remove your < p > using the .remove() function.

Why doesn't it pass when I call it on $p ? What's different?

Sorry, my goal is to get a solid understanding of all of this and not just keep passing level after level.


$(document).ready(function(){
    $("#one").after("<p>Test Test</p>");
    var $p = $("p");
    $("#two").after($p);
    
    $p.remove();
});


#2

Hi you should remove your element like that

$('element to remove').remove();

#3

Is that because that's the "right" way to pass this step?

I'm only a bit confused because the "p" element does get removed from the html viewer when you hit full screen. So is it "technically" correct the way I did? But just codecadamy doesn't accept that as an answer?

Or do I have it completely wrong here?


#4

I think that when you do that

$p.remove();

you are removing the variable var $p = $("p"); which make the same effect as removing the tag but the lesson want you to remove the tag so you should use the one I gave you.


#6

What I fail to understand is why this needs to be part of the function, surely this can also be a stand-alone line on text, in the real world, if something needs to be removed it is never normally directly after the creation of it?

my code:

$(document).ready(function() {
    $('#one').after("<p>This is mine</p>");

//var $paragraph = $("p");
$('#two').after($('p'));
});

$('p').remove();

(I have corrected it to move forward but I am not sure I learnt anything)


#7

@snox2014 That won't work; you're removing all <p> elements with your last line before you ever add any, on your second line.

In the real world, either the <script> tags go in the bottom of the HTML document, or everything goes inside the $(document).ready(...); function or some equivalent of it.