/    Sign up×
Community /Pin to ProfileBookmark

Using a button on a form

Hello,

Background:
I have a application I am building that has a input box, and a button (not a submit button). When you click the button, it calls a javascript function which processes the text in the input box, and then clears it so you can enter text again.

Problem:
It works great if you click the button with a mouse. But I want to be able to hit enter, and still call the javascript function, without reloading the page. I dont want it to act as a normal form. The page should never need to reload.

[code=php]
function myfunction() {
var myvariable = document.inputForm.myvariable.value;
//Process myvariable
//Clear myvariable textfield
document.inputForm.myvariable.value = ”;
}

<form name=”inputForm” onSubmit=”myfunction();”>
<input type=”textfield” name=”myvariable” style=”width:100%;”>
<input type=”button” value=”Process” onclick=”myfunction();”>
</form>
[/code]

-Jason

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisSep 19.2006 — <i>
</i>function myfunction() {
var myvariable = document.inputForm.myvariable.value;
//Process myvariable
//Clear myvariable textfield
document.inputForm.myvariable.value = '';
[color=red]return false;[/color]
}

&lt;form name="inputForm" onSubmit="[color=red]return [/color]myfunction();"&gt;
&lt;input type="textfield" name="myvariable" style="width:100%;"&gt;
&lt;input type="button" value="Process" onclick="myfunction();"&gt;
&lt;/form&gt;
×

Success!

Help @jrottman 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 6.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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...