Yankee Web Code- Run.zap()

by Kenneth Tibbetts

Content and Control Sometimes you need to remove an element from the document. Call on Run.zap() to vaporize an element, or to remove its children and leave it empty.

What's a Run.zap?

Arguments: 1 required, unlimited optional

Returns: nothing

Arguments:

Run.zap(hoo)

index required description type
(hoo) [0] yes an html element string or object
[1],[2],[3]... no html elements strings or objects


Calling Run.zap()

In its simplest form, Run.zap() removes an element's content but not the element itself. If you Run.zap() a paragraph element, all of its children will be removed from the document, but the paragraph itself will still be in the source, and new content may be added anytime. 

Sometimes you do want to keep empty elements in the document flow, to maintain the relations between the rest of the page elements. You can call on a nodeSort item, for instance, without having to recalculate everything. 

If you remove document.getElementsByTagName('p')[0]; the first paragraph will lose it's content, but the second p element will still be document.getElementsByTagName('p')[1].If the element has an id of 'p_3', for example:

Run.zap('p_3');

 

Often you will remove the element itself along with its contents. 

Run.zap('p_3!');

The exclamation point appended to the id string forces the element itself to be removed.

You can remove several elements at once, one per argument:

Run.zap('p_3!','h2_2!','p_5!','img_6!');

If you want to remove an element with an unknown id, you can use Run.hoozit() to find or generate one:

Run.zap(Run.hoozit(getElementsByTagName('p')[2])+'!' );

Here Run.hoozit() checks for an id for the third paragraph (as indexed from 0). If there is no id attribute for the element, a unique id is generated. The id string is then concatenated with the '!' to remove the element along with its content.

Run.zap() Source

function Run.zap(){

var a= arguments;

var wipeout= false;

var who;

/*  a is used as a shortcut for the arguments array. wipeout is a boolean to trigger whether or not the element itself will be deleted along with its content.  */

for(var i= 0; i< a.length; i++){

wipeout= false;

if(typeof(a[i]) == 'string' && a[i].end('!')){

who= mr(a[i].slice(0,-1));

wipeout= true;

/*  If the '!' is appended to the element id, wipeout is set to true and the exclamation point is sliced off the element's id string.  */

}

else who= mr(a[i]);

if(who== false) continue;

 /*  If the argument is an object reference, find out if it is an existing element. If you already zapped this guy, continue the loop for the next argument.  */

while(who.lastChild !=null) who.removeChild(who.lastChild);

 /*  The while loop removes each child in turn, as long as there are any childNodes remaining.  */

var pa= who.parentNode;

if(wipeout && pa ) pa.removeChild(who);

/*  If we set the element for removal you need to find its parent. You have to be in the context of the element's parent to remove it. Repeat until all the arguments have been processed. There is no return value.  */

}

}

 

What Else?

    Code from the Yankee Webshop
  1. Practical Code
  2. A DOM Function Library
  3. mr(hoo)
  4. Run.dr(wot)
  5. Run.mrs(hoo)
  6. Run.doRight()
  7. Run.sayWhat()
  8. Run.zap()

Internet Resources:

e- mail: editor@yankeeweb.net

WebShop Privacy Policy