SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jun 2004
    Location
    Prague, Czech rep.
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Removing label from a form

    How can I remove label from this form? This code doesn´t work:

    <script>
    function remove() {
    document.forms[0].removeChild(document.forms[0].getElementByID("age"))}
    }
    </script>
    <form onsubmit="remove()">
    <fieldset>
    <legend>Some legend</legend>
    <label>Name </label><input type="text" />
    <label>Surname </label><input type="text" />
    <label id="age">Age </label><input type="text" />
    <input type="submit" value="Submit" />
    </fieldset>
    </form>

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For starters...you're doing this onsubmit, so the current document - the one you're altering - is about to go bye-bye. What was the idea exactly?

    Errors:

    1) getElementByID ----> must be lower-case
    2) document.forms[0].getElementById ----> no such animal, it's a method of document
    3) That label is a child of FIELDSET, not FORM
    4) ...].getElementByID("age"))} ----> syntax (extra curly)
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" 
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    <head>
    <title>untitled</title>
    <script type="text/javascript">
    
    function remove()
    {
    	var oForm = document.getElementsByTagName('form')[0];
    	oForm.firstChild.removeChild(document.getElementById('age'));
    	return false; //cancels submission
    }
    
    </script>
    </head>
    <body>
    <form onsubmit="return remove()">
    <fieldset>
    <legend>Some legend</legend>
    <label>Name </label><input type="text" /> 
    <label>Surname </label><input type="text" /> 
    <label id="age">Age </label><input type="text" /> 
    <input type="submit" value="Submit" />
    </fieldset>
    </form>
    </body>
    </html>
    ::: certified wild guess :::


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •