Jump to content

Basic javascript form question


Buyocat

Recommended Posts

Let me start by saying I do not want to validate the form.  What I want is to allow a user to submit the form by hitting enter in the same way that he can by pressing the submit button.  I have the following code:

 

<form id="chatbox" action="" onsubmit="makeRequest()">

<fieldset>

<legend>Instant Message</legend>

<input type="hidden" name="action" value="append" />

<input type="text" name="line" size="80" />

<input type="button" value="Enter" onClick="makeRequest()" />

</fieldset>

</form>

 

It correctly works when the button is pressed.  By correct I mean it calls the appropriate script, receives the callback, and then reloads the page, which is what I wanted it to do for the time being.  But when I press enter it simply reloads the page with get data in the ur, i.e. it does not call makeRequest.  Can someone tell me how to have makeRequest called when enter is pressed, too?

 

Thanks Buyo

Link to comment
Share on other sites

While I know next to nothing about Javascript I was under the impression that when a form was submitted regardless of how a "On Submit" event was fired that could be detected before everything else went into motion.

 

http://www.w3schools.com/jsref/jsref_onsubmit.asp

 

Would it be possible to insert some Javascript in the page that listened for that event?  Or does any Javascript that works in that instance need to be inline with the form?

Link to comment
Share on other sites

The following works erratically.  That is on some occasions it works as expected but on others it fails by either doing nothing (i.e. not refreshing the page as expected, but successfully calling the script) or by calling the page incorrectly as if the javascript weren't being called.

 

<form name="chatbox" id="chatbox" action="./" onsubmit="makeRequest()">

 

makeRequest looks like:

function makeRequest()

{

var form = "chatbox";

var target = "./index.php";

postForm(target,form);

}

Link to comment
Share on other sites

This is what I do that works:

--add 'return false;' to your onClick statement in the button:

<input type="button" value="Enter" onClick="makeRequest();return false;" />

 

--remove the onSubmit from the form tag:

<form id="chatbox" action="">

--makeRequest() is activated when ENTER is pressed or when the button is clicked

--makeRequest() can use ajax or can use document.formname.submit();

--works both in ie and ff

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.