www.webdeveloper.com
Results 1 to 11 of 11

Thread: is there another way is declaring the elements

  1. #1
    Join Date
    Feb 2012
    Posts
    9

    is there another way is declaring the elements

    I have tried the get element by Id (and declared id ofcourse)...but all seems to fails

    so I have a form and has 4 inputs..the javascript for it is:-
    function reset2(myform)
    {
    document.forms[0].elements[0].value = "";
    document.forms[0].elements[1].value = "";
    document.forms[0].elements[2].value = "";
    document.forms[0].elements[3].value = "";
    return true;
    }

    Is there another way to declare :-
    document.forms[0].elements[0].value = "";

    Let me know. An advance thank you for helping.
    ~PR

  2. #2
    Join Date
    Mar 2011
    Posts
    1,109
    I wouldn't recommend using array indexes (indeces) to get at form elements. Your page could change or your form could change and you'd be looking at lots of changes. Use element name attributes to address individual elements:

    document.forms['formName'].elementName.value = '';
    Rick Trethewey
    Rainbo Design

  3. #3
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    In this case you might just use Form.reset().
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  4. #4
    Join Date
    Feb 2012
    Posts
    9

    not working for some reason

    The changed code:-

    function reset2(myform)
    {
    document.forms['myform'].name.value = "";
    document.forms['myform'].email.value = "";
    document.forms['myform'].pwd.value = "";
    document.forms['myform'].cpwd.value = "";
    return true;
    }

    the html:-
    <p>Name</p>
    <input type="text" name="name" id="name" />
    <p>Email Address</p>
    <input type="text" name="email" id="email" />
    <p>Password</p>
    <input type="text" name="pwd" id="pwd" />
    <p>Confirm Password</p>
    <input type="text" name="cpwd" id="cpwd" />

    The earlier (array) code works but this doesn't...
    ~PR

  5. #5
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    You're going to have to post the whole page worth of HTML and JavaScript.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  6. #6
    Join Date
    Mar 2006
    Location
    Discworld
    Posts
    75
    What exactly does not work in this code? I tested this in IE,FF and Chrome. Working just fine unless you screw up somewhere...

    <html>
    <head>
    <script>
    function reset2(myform)
    {
    document.forms['myform'].name.value = "";
    document.forms['myform'].email.value = "";
    document.forms['myform'].pwd.value = "";
    document.forms['myform'].cpwd.value = "";
    return true;
    }
    </script>
    </head>
    <body>
    <form name="myform" method="post">
    <p>Name</p>
    <input type="text" name="name" id="name" />
    <p>Email Address</p>
    <input type="text" name="email" id="email" />
    <p>Password</p>
    <input type="text" name="pwd" id="pwd" />
    <p>Confirm Password</p>
    <input type="text" name="cpwd" id="cpwd" />
    <p><input type=button onclick="reset2(this)" value="Reset"></p>
    </body>
    </html>

  7. #7
    Join Date
    Feb 2012
    Posts
    9
    sorry I had the form name wrong...oops! ..it works..thanx all

  8. #8
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    It's what I thought, just use a reset button. HTML already has this functionality built in. There is no need for yucky, inconsistent JavaScript. http://www.w3.org/TR/html4/interact/...-control-types
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  9. #9
    Join Date
    Mar 2006
    Location
    Discworld
    Posts
    75
    Hey, you never know what else he is going to use this form, not just a reset as I understand...
    before you start avoiding something learn how to use it first.. ;-)

  10. #10
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    Quote Originally Posted by alexsts View Post
    Hey, you never know what else he is going to use this form, not just a reset as I understand...
    before you start avoiding something learn how to use it first.. ;-)
    Forgive me, I mistook your example for his problem. I was the atrocious HTML that confused me.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  11. #11
    Join Date
    Feb 2012
    Posts
    9
    In one place you say u dont like the javascript...and in other u say u dont like the html...i think u need to understand the difference between the two first. No need of your views on -you like this code or that...and whts with the dictionary words!!!!!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles