SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    Denver
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Elements per HTML table

    Hello guys,

    I was wondering if there is a way in Javascript to clear form elements inside a particular HTML table.
    Let's say that I have 1 form on my page and my form elements (input, textarea, etc..) are residing into different HTML tables all over the page.

    Is there a way for me to write a javascript function and pass in the id of a particular table and clear (or apply a set of rules.. such as setting the .value='') JUST on the elements inside that specific table. Thanks in advance for the help.

  2. #2
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Parry Sound, ON
    Posts
    725
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    An easier way to approach this, if you are not already using the class attribute on those form elements for CSS purposes, might be to give them a class name and use that to filter a getElementsByTagName() call. Otherwise you're into some recursive childNodes stuff.

    Something like
    Code:
    function clrForm(cName)
    {
        var els = ['INPUT','TEXTAREA'];
        for(var i=0;i<els.length;i++)
        {
            inputs = document.getElementsByTagName(els[i]);
            for(var j=0;j<inputs.length;j++)
            {
                if(inputs[j].className == cName)
                {
                    inputs[j].value = '';
                }
            }
        }
    }
    Not tested at all, but might be one way to go.
    Last edited by HardCoded; Mar 31, 2005 at 03:57.

  3. #3
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    Denver
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HardCoded,

    Since I'm already using the Class attr. I thougth about assigning a custom HTML attribute (ex. group = "1") to my <input> or <textarea> elements. I'm just looking around for better ideas before I go ahead and do that. Thanks for your help.

  4. #4
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Parry Sound, ON
    Posts
    725
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good plan, but will it validate? If you care about validation, you might use two class names
    i.e.
    HTML Code:
    <input class="reset1 bluebg" />
    Then you can do this instead:
    Code:
    if(inputs[i].className.substring(0,6) == cName)

  5. #5
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by HardCoded
    Code:
    function clrForm(cName)
    {
        var els = ['INPUT','TEXTAREA'];
        for(var i=0;i<els.length;i++)
        {
            inputs = document.getElementsByTagName(els[i]);
    This is basically correct, but we need only tags inside some html element, not in the whole document. Therefore

    inputs = table_in_question.getElementsByTagName(els[i]);

  6. #6
    SitePoint Evangelist SpaceFrog's Avatar
    Join Date
    Jan 2005
    Location
    Outerspace
    Posts
    511
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    var els = ['INPUT','TEXTAREA'];
    for(var i=0;i<els.length;i++)
    {
    inputs = document.getElementsByTagName(els[i]);
    would you have to push array ???
    PHP Code:
     for(var i=0;i<els.length;i++)
        {
            
    inputs.push(document.getElementsByTagName(els[i])) 
    ;

  7. #7
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    Denver
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think stereofrog's note solves my problem.

    Code:
    myTableName.getElementsByTagName('INPUT');
    Thanks for the help guys.

  8. #8
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Parry Sound, ON
    Posts
    725
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, indeed. I had no idea you could do that.

  9. #9
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    Denver
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You learn something new everyday. This is what makes SitePoint the best


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
  •