?
Solved

Both Enter key or onClick button to run a function.

Posted on 2005-04-25
17
Medium Priority
?
176 Views
Last Modified: 2012-05-05
I am no expert. I have been trying to make this work for hours. I can't get onKeyPress to work in this case.

The code and password page can be found at http://www.leebay.com. "pass" as the password makes it go to http://www.leebay.com/pass.htm.

Please help...
0
Comment
Question by:fartiemc
  • 8
  • 5
  • 3
  • +1
17 Comments
 
LVL 25

Expert Comment

by:archrajan
ID: 13859034
Try this:
<html>
<head>
<meta http-equiv="Content-Language" content="en-us">

<SCRIPT language="JavaScript">

function Guardian() {

        var password = document.form1.password.value;
       var temp = 'http://www.leebat.com/'+password+'.htm';
         document.form1.action = temp;
         document.form1.submit();
      //self.location.replace('http://www.leebay.com/' + password + '.htm')
        }
function Guardian2()
{
var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
 if (keyCode == 13) {
   Guardian();
 }
//return false;
}
document.onkeypress = Guardian2;
     </SCRIPT>
       </head>

<body>

<p align="center">



</p>
<p align="center">&nbsp;</p>
<p align="center">
&nbsp;</p>
<form name="form1" method = "post">

<div align="center">
  <center>

<TABLE BORDER=0 width="553" style="border-collapse: collapse" bordercolor="#111111" cellpadding="0" cellspacing="0">

<TR>

      <TD width="550">
    <p align="center"> <input type="password" name="password" size=45>&nbsp;
    <input type="button" value="      ENTER      " name="enter" onclick="Guardian()" ></TD size="20">


 
</script>

<input name="Check Answers" type="button" value="Check Answer" onKeyPress="return handleEnter()" onClick="fillBlanks()">
</TR size="20">

</TABLE></center>
</div>
</body>
</html>
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 13859090
I would change the button to this:

<input type="submit" value="      ENTER      " name="enter"  >

The form tag should be then like this:

<form name="form1" onSubmit="return Guardian(this)">

And the function looks like this:

function Guardian(theForm) {
        var pwd = theForm.password.value;
        location.href = '/' + pwd + '.htm';
        return false;
}

0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 13859117
I dont see the form ending tag ( </form> ) !?

and how about this:

<html>
<body>
<form name="form1" method="POST">
<input type="password" name="password" value=""/>
<input type="submit" value=" ENTER " onclick="this.form.action=this.form.password.value+'.htm'"/>
</form>
</body>
</html>
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:fartiemc
ID: 13859123
I got:

Method Not Allowed
The requested method POST is not allowed for the URL /pass.htm.


--------------------------------------------------------------------------------

Apache/1.3.31 Server at www.leebay.com Port 80

When i used archrajan's code.
0
 

Author Comment

by:fartiemc
ID: 13859335
I can't get anything to work correctly when I change the "button" to input type="submit".
0
 
LVL 25

Expert Comment

by:archrajan
ID: 13859392
In the code i gave try to change
<form name="form1" method = "post">
to
just

<form name = "form1">
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 13859413
<html>
<body>
<form name="form1" method="POST">
<input type="password" name="password" value=""/>
<input type="button" value=" ENTER " onclick="this.form.action=this.form.password.value+'.htm'; this.form.submit();"/>
</form>
</body>
</html>
0
 

Author Comment

by:fartiemc
ID: 13859613
archrajan, it works!!!

I just posted it at http://www.leebay.com/

It seems to work great.

Anyone see any problems with this?
0
 
LVL 25

Assisted Solution

by:archrajan
archrajan earned 600 total points
ID: 13859635
Does that work with firefox???

if it does not
include along with this line

document.onkeypress = Guardian2;


to

document.onkeypress = Guardian2;
document.onkeyup = Guardian2;
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 13859717
>> Anyone see any problems with this?

What is the need for onkeypress event or Guardian2 function ?

Have you tried my code? Is it not the same with minimized code?
0
 

Author Comment

by:fartiemc
ID: 13859806
archrajan, with or without document.onkeyup = Guardian2; pressing enter does not work in firefox. Any suggestions?

ldbkutty, pressing enter does not seem to work with that code. onClick works but not enter.

Thanks again and again to you both.
0
 
LVL 32

Accepted Solution

by:
ldbkutty earned 1400 total points
ID: 13860021
More problems:

There is no </TR> tag.
Unusual </script> in the middle of the HTML.
Unusual <align="center"> statment.
No </p> for    <p align="center">.

This works for me in IE and Firefox:

<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<script>
function submitForm() {
 var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
 if(keyCode == 13) {
  document.form1.action = document.form1.password.value+'.htm';
  document.form1.submit();
 }
}
</script>
</head>
<body onkeypress="submitForm();">
<br>Test Password is "pass"<br>
<form name="form1" method="POST" action="" onsubmit="this.form.action=this.form.password.value+'.htm';">
<div align="center">
<TABLE BORDER="0" width="553" style="border-collapse: collapse" bordercolor="#111111" cellpadding="0" cellspacing="0">
 <TR>
  <TD width="550">
   <input type="password" name="password" size="45"/>&nbsp;
   <input type="submit" value="      ENTER      " name="enter" onclick="this.form.action=this.form.password.value+'.htm'; this.form.submit();"/>
  </TD>
 </TR>
</form>
</TABLE>
</div>
</body>
</html>
0
 

Author Comment

by:fartiemc
ID: 13860573
ldbkutty, that works good. I had to take method="POST" out for it to work, is this ok? It works in both ie and firefox.

Take a look at http://www.leebay.com/index3.htm

Also is http://www.leebay.com/index4.htm the correct way to make it go back a few folders to get to the file?

Thanks.



0
 

Author Comment

by:fartiemc
ID: 13860605
Actually, http://www.leebay.com/index4.htm does not go to the write page if enter is pressed so it must not be right.
0
 

Author Comment

by:fartiemc
ID: 13860647
This seems to work for both ways...

http://www.leebay.com/index5.htm

Let me know if this is a good way to do it.

Thanks.
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 13860803
This is better and simple to change the action page if required.

<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<script language="JavaScript" type="text/javascript">
var actionPage = "http://www.leebay.com/whatever/whatever/";
function submitForm() {
 var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
 if(keyCode == 13) {
  document.form1.action = actionPage+document.form1.password.value+'.htm';
  document.form1.submit();
 }
}
</script>
</head>
<body onkeypress="submitForm();">
<br>Test Password is "pass"<br>
<form name="form1" action="" onsubmit="this.form.action=actionPage+this.form.password.value+'.htm';">
<div align="center">
<TABLE BORDER="0" width="553" style="border-collapse: collapse" bordercolor="#111111" cellpadding="0" cellspacing="0">
 <TR>
  <TD width="550">
   <input type="password" name="password" size="45"/>&nbsp;
   <input type="submit" value="      ENTER      " name="enter" onclick="this.form.action=actionPage+this.form.password.value+'.htm'; this.form.submit();"/>
  </TD>
 </TR>
</form>
</TABLE>
</div>
</body>
</html>
0
 

Author Comment

by:fartiemc
ID: 13860897
Thank you very much.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question