Make sure you remove your <p> using the .remove() function


#1

Does anyone knows why this code dont pass on the validation?

$(document).ready(function(){
var $p = $("

Hello from the outside

");
$("#one").after($p);
var $two = $("#two");
($two).after($p);
($p).remove();
});

Thanks in advance :wink:


#2

Please re-edit your Post

  • leave one blank-line above of your code
  • select your code in the Post
  • then =click= on the </>-symbol-of-this-editor

Your code will then be in a pre-code state
and you will be able to make/present the proper indentations.


#3

When you change

$($p).remove();
to

$('p').remove();

it does accept it.
However, doesn't this mean that it removes all p objects from the code? I think this shouldn't be correct.


#4

@toewan,
Your assumption is true

 $('p').remove();

will remove all HTML-Elements with a p-Tag

as it is not visible in your Original Post what you are assigning to $p
i cannot be of further help....


#5

The definition of $p was as follows:

var $p = $('<p>Text</p>');

As the p element disappeared in the editor, I assume this is the correct way to do it. It didn't accept it, however.

My full code was:

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


#6

@toewan,

should be inside of your document =ready= Eventhandler

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