Removing a element (DOM/JS)

Discussion in 'Javascript' started by Jam Pa, May 26, 2005.

  1. Jam Pa

    Jam Pa Guest

    Ok I am one of those unfortunate souls who have to make a text editor for
    CMS.

    So, I need to remove a <textarea> on a page (this is for non javascript
    browsers).

    I can figure out the element (getLelementById), but so far have been unable
    to remove it. I tried to set the elements innerHTML to "". Interestingly,
    in firefox the element is doubled (1.0.4).

    anyone have any help with this. I have ie support to work on too, so *any*
    pointers for *any* browsers on replacing elements with javascript are very
    very welcome.
    Jam Pa, May 26, 2005
    #1
    1. Advertising

  2. Jam Pa wrote:

    > So, I need to remove a <textarea> on a page (this is for non javascript
    > browsers).
    >
    > I can figure out the element (getLelementById), but so far have been unable
    > to remove it.


    if (element.parentNode && element.parentNode.removeChild) {
    element.parentNode.removeChild(element);
    }

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, May 26, 2005
    #2
    1. Advertising

  3. Jam Pa

    Jam Pa Guest

    Martin Honnen <> wrote in
    news:4295fcef$0$25684$-online.net:

    >
    > Jam Pa wrote:
    >
    >> So, I need to remove a <textarea> on a page (this is for non
    >> javascript browsers).
    >>
    >> I can figure out the element (getLelementById), but so far have been
    >> unable to remove it.

    >
    > if (element.parentNode && element.parentNode.removeChild) {
    > element.parentNode.removeChild(element);
    > }


    Thanks for your reply.

    The code below works.. at times. And sometimes it wont. Go figure...

    <div id="supa">
    <p>ldakjsdlkjaslkjsad</p>
    <p>etc</p>
    <textarea id="tarea"></textarea>
    <script language="JavaScript" type="text/javascript">
    function removeElementById() {
    parn = "supa";
    rele = "storyarea";
    pare = document.getElementById(parn);
    rele = document.getElementById(rele);
    fstatus = "";

    if (tarea.parentNode && tarea.parentNode.removeChild
    (tarea)) { tarea.parentNode.removeChild(tarea); }
    else { alert("Nada!"); }
    fstatus = "jaa";
    return fstatus;
    }
    removeElementById();
    </script>
    </div>
    Jam Pa, May 26, 2005
    #3
  4. Jam Pa

    Jam Pa Guest

    Ok well this is how Im working this script in 'final' (lol):

    function removeElementById(remele) {
    remele = document.getElementById(remele);
    if (remele.parentNode && remele.parentNode.removeChild(remele)) {
    remele.parentNode.removeChild(remele); }
    }

    removeElementById("storyarea");

    I hope this may help someone out there
    Jam Pa, May 26, 2005
    #4
  5. Jam Pa

    RobG Guest

    Jam Pa wrote:
    > Ok well this is how Im working this script in 'final' (lol):


    Please don't post code with tabs, replace them with spaces. It nearly
    always
    causes wrapping that introduces errors.

    >
    > function removeElementById(remele) {
    > remele = document.getElementById(remele);
    > if (remele.parentNode && remele.parentNode.removeChild(remele)) {


    Here you will actually call the removeChild method, not just test it.
    The intention is to test the method, and if it's supported, call it:

    if ( remele.parentNode && remele.parentNode.removeChild ) {

    > remele.parentNode.removeChild(remele); }
    > }



    If the method is not supported, you will get an error from the if(..),
    if the method is supported, you will get an error from this line
    because 'remele' was already removed in the test.

    >
    > removeElementById("storyarea");
    >
    > I hope this may help someone out there


    I hope this helps you! 8-p

    --
    Rob
    RobG, May 27, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    1,170
    Tom Regner
    Jul 15, 2004
  2. Chris  Chiasson
    Replies:
    6
    Views:
    612
    Richard Tobin
    Nov 14, 2006
  3. HANM
    Replies:
    2
    Views:
    706
    Joseph Kesselman
    Jan 29, 2008
  4. Stanimir Stamenkov

    DOM: Re-inserting an element without removing it first

    Stanimir Stamenkov, Apr 22, 2004, in forum: Javascript
    Replies:
    1
    Views:
    89
    Martin Honnen
    Apr 22, 2004
  5. ted benedict
    Replies:
    3
    Views:
    144
Loading...

Share This Page