Removing elements


Why is not the following code being accepted ? The link is:

$(document).ready(function() {
    $('#one').after("<p>Hello Anuja</p>");
    var $p = $('p');


While valid, it goes beyond what the exercise expects. No harm, just give it what it wants.



There really is no reason to cache the jQuery object in this exercise. A transient selector is all we need.

When creating a jQuery object with a p selector, it builds a collection of all the P-nodes in the DOM. We refer to that as a node list.but it is much more than a normal list. It’s a JavaScript collection of every selector node type and all their children. It’s a list because there are many possible parent nodes,

Because of the wide coverage of a node type selector, they are the least desirable to have in our code, if not specifically suited to the design. That is where class comes in so we can isolate a select group of node type elements that are related by marriage, not as family. They all have the same class.

Where it gets interesting is when we create an element of any node type, P-node or any other, and make it a jQuery object.

$p = $('<p/>').text("I'm a sprite");

Now you have an object that resides in memory and is dynamic in insertion and removal, as well in text. Both can be changed on the fly anytime during a session.

Take some time to play with this idea and it will give you some practice with the before and after methods, as well as many others. You’ll have fun, guaranteed. Sprites are fun.


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