[thelist] DOM: removing appended text

Matt Warden mwarden at gmail.com
Fri Dec 9 15:24:36 CST 2005


On 12/9/05, fstorr <fffrancis at fstorr.demon.co.uk> wrote:
> Hi all
>
> I've put together a little function to take user input from a text
> field and append it to the end of a paragraph.  What I'm stuck on is
> how to also remove said text.  This is what I've got:
>
> function updatename(action)
> {
>         var quotefor = document.getElementById('quotefor'); // text entry
> form field
>         var quoteename = document.getElementById('quoteename'); // the
> targat <p> tag
>         var quote = document.createTextNode(quotefor.value);
>
>         if (action == 'update)
>         {
>                 quotename.appendChild(quote);
>                 return false; // cancels some things further down the page
>         }
>
>         if (action == 'remove')
>         {
>                 // can't get anything to work here!
>         }
>
> }

Something like this (untested):

function updatename(action)
{
       var quoteename = document.getElementById('quoteename');

       if (action == 'update') {
              var quotefor = document.getElementById('quotefor');
              var quote = document.createTextNode(quotefor.value);
              quotename.appendChild(quote);
              return false;
       }

       if (action == 'remove') {
              var childnodes = quotename.childNodes;
              for (var i=0; i<childnodes.length; i++) {
                     if (childnodes[i].nodeType==3) {
                            quotename.removeChild(childnodes[i]);
                            break;
                     }
              }
              return false;
       }

}

This assumes there is only one child node of type text.

--
Matt Warden
Miami University
Oxford, OH, USA
http://mattwarden.com


This email proudly and graciously contributes to entropy.



More information about the thelist mailing list