/    Sign up×
Community /Pin to ProfileBookmark

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

to post a comment
HTML

10 Comments(s)

Copy linkTweet thisAlerts:
@rtretheweyFeb 08.2012 — 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 = '';
Copy linkTweet thisAlerts:
@CharlesFeb 08.2012 — In this case you might just use [font=monospace]Form.reset()[/font].
Copy linkTweet thisAlerts:
@pr_longhornauthorFeb 08.2012 — 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
Copy linkTweet thisAlerts:
@CharlesFeb 08.2012 — You're going to have to post the whole page worth of HTML and JavaScript.
Copy linkTweet thisAlerts:
@alexstsFeb 08.2012 — 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>
Copy linkTweet thisAlerts:
@pr_longhornauthorFeb 08.2012 — sorry I had the form name wrong...oops! :eek:..it works..thanx all
Copy linkTweet thisAlerts:
@CharlesFeb 08.2012 — 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/forms.html#input-control-types
Copy linkTweet thisAlerts:
@alexstsFeb 08.2012 — 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.. ;-)
Copy linkTweet thisAlerts:
@CharlesFeb 08.2012 — 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.. ;-)[/QUOTE]
Forgive me, I mistook your example for his problem. I was the atrocious HTML that confused me.
Copy linkTweet thisAlerts:
@pr_longhornauthorFeb 08.2012 — 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!!!!!
×

Success!

Help @pr_longhorn spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...