|
-
Dec 29th, 2001, 10:39 PM
#1
Thread Starter
Member
Simple Javascript
I've got a form with a textbox. When I press Enter in the textbox, I don't want the form to submit; instead I want to call a JS function. How would I do that?
-
Dec 29th, 2001, 10:52 PM
#2
for IE anyway:
Code:
fucntion Text1_KeyPress()
{
if (event.keyCode == 13)
{
event.returnValue = false;
//call JS function here.
}
}
<form>
<input type="text" onKeyPress="Text1_KeyPress()"/>
</form>
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Dec 29th, 2001, 10:53 PM
#3
Thread Starter
Member
Does it work in NS or have you just not tested it?
-
Dec 29th, 2001, 10:55 PM
#4
Originally posted by filburt1
Does it work in NS or have you just not tested it?
Untested. I don't think I've ever tested for cross-browser compatability, I just hope for the best.
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Dec 29th, 2001, 11:02 PM
#5
Thread Starter
Member
Well it works great in IE but surprise surprise, NS managed to screw it up. But there's nothing in the JS Console ("javascript:" in the location bar), it just does nothing making me assume "onKeyPress" isn't supported:
Code:
<script language="javascript">
<!--
function transferKeyEvent()
{
if (event.keyCode == 13)
{
event.returnValue = true;
submitSearch();
}
}
// -->
</script>
<input width="100%" type="text" onKeyPress="transferKeyEvent()" name="q">
-
Dec 29th, 2001, 11:06 PM
#6
Well, from what I understand, NS has a totally different event handling scheme than IE, so, it wouldn't be that its not supported, just called in a different way. I haven't coded for NS in a long while, so it could have changed since then.
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Dec 29th, 2001, 11:10 PM
#7
Originally posted by filburt1
Well it works great in IE but surprise surprise, NS managed to screw it up. But there's nothing in the JS Console ("javascript:" in the location bar), it just does nothing making me assume "onKeyPress" isn't supported:
Code:
<script language="javascript">
<!--
function transferKeyEvent()
{
if (event.keyCode == 13)
{
event.returnValue = true;
submitSearch();
}
}
// -->
</script>
<input width="100%" type="text" onKeyPress="transferKeyEvent()" name="q">
On second look, you are setting event.returnValue to True, try setting it to False.
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Dec 29th, 2001, 11:11 PM
#8
Thread Starter
Member
Originally posted by crptcblade
On second look, you are setting event.returnValue to True, try setting it to False.
Good point but no change.
-
Jan 2nd, 2002, 04:31 AM
#9
Hyperactive Member
This thread is going in the wrong direction.
Here is the answer to the question
Code:
<script>
function checkMyForm(the_form){
//do some errror checking
//if everything is ok
if(the_form.mytext.value == "hello world"){
the_form.submit();
//else if we need more input
}else{
alert("please enter the correct phrase !");
}
return false;
}
</script>
<form name="myform" method="post" action="some.cgi" onSubmit="return checkMyForm(this)">
<input type="text" name="mytext">
</form>
This is cross browser compatiable and works a treat.
If you need me to explain how this method works and what it is doing just ask !!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|