Go Back   CodingForums.com > :: Client side development > HTML & CSS

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-18-2005, 05:54 PM   PM User | #1
hourang
New Coder

 
Join Date: Jul 2005
Location: detroit area
Posts: 75
Thanks: 0
Thanked 0 Times in 0 Posts
hourang is an unknown quantity at this point
pressing enter in <textarea> box

hi all, i have a form that has a <textarea> box. when a user presses enter i want it to submit the form instead of breaking to the next line and inserting a cariage return in the form data. i do not have a submit button and instead use a image with javascript link, do i have to use a submit button for this to work? this is probably better put in the javascript section. thanks.
hourang is offline   Reply With Quote
Old 07-18-2005, 06:29 PM   PM User | #2
_Aerospace_Eng_
Supreme Master coder!


 
_Aerospace_Eng_'s Avatar
 
Join Date: Dec 2004
Location: In a place far, far away...
Posts: 19,292
Thanks: 2
Thanked 1,044 Times in 1,020 Posts
_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript">           
function ifEnter(field,event) {
var theCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
if (theCode == 13){
document.forms[0].submit();
return false;
} 
else
return true;
}      
</script>
</head>

<body>
<form>
<textarea cols="10" rows="5" onkeypress="ifEnter(this,event);"></textarea>
<input type="image" src="someimage.jpg">
</form>
</body>
</html>
_Aerospace_Eng_ is offline   Reply With Quote
Old 07-18-2005, 06:38 PM   PM User | #3
]|V|[agnus
Regular Coder

 
Join Date: May 2004
Location: Minneapolis, MN, USA
Posts: 904
Thanks: 0
Thanked 0 Times in 0 Posts
]|V|[agnus is an unknown quantity at this point
This is a JS question (well, and so was the original) but...

Aero, could you explain this line a little more:

Code:
var theCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
I am familiar with this syntax:

Code:
var someVar = (...some condition to check...) ? valueIfTrue : valueIfFalse;
Your syntax has some extra details I'm not familiar with.
__________________

Opposite of Sequitur
]|V|[agnus is offline   Reply With Quote
Old 07-18-2005, 06:46 PM   PM User | #4
hourang
New Coder

 
Join Date: Jul 2005
Location: detroit area
Posts: 75
Thanks: 0
Thanked 0 Times in 0 Posts
hourang is an unknown quantity at this point
Quote:
Originally Posted by _Aerospace_Eng_
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript">           
function ifEnter(field,event) {
var theCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
if (theCode == 13){
document.forms[0].submit();
return false;
} 
else
return true;
}      
</script>
</head>

<body>
<form>
<textarea cols="10" rows="5" onkeypress="ifEnter(this,event);"></textarea>
<input type="image" src="someimage.jpg">
</form>
</body>
</html>
awesome thats what i need. now what about the shift-enter thing? what would the charcode be for something like that? and what about users with js disabled(not worried about it just curious)?
hourang is offline   Reply With Quote
Old 07-18-2005, 06:49 PM   PM User | #5
Grant Palin
Regular Coder

 
Join Date: Jun 2002
Location: Victoria, BC, Canada
Posts: 962
Thanks: 0
Thanked 1 Time in 1 Post
Grant Palin is an unknown quantity at this point
Quote:
Code:
var theCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
I am familiar with this syntax:

Code:
var someVar = (...some condition to check...) ? valueIfTrue : valueIfFalse;
Your syntax has some extra details I'm not familiar with.
That's a compound statement - it uses two, nested, ternary operators. It's clearer when you add parentheses:

Code:
var theCode = (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode));
So if the first test (event.keyCode) fails, the second value is used, which is itself the result of another ternary operator. If the second test (event.which) fails, the final value (event.charCode) is used.
Grant Palin is offline   Reply With Quote
Old 07-18-2005, 06:49 PM   PM User | #6
_Aerospace_Eng_
Supreme Master coder!


 
_Aerospace_Eng_'s Avatar
 
Join Date: Dec 2004
Location: In a place far, far away...
Posts: 19,292
Thanks: 2
Thanked 1,044 Times in 1,020 Posts
_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light
Grant said it quite well.
_Aerospace_Eng_ is offline   Reply With Quote
Old 07-18-2005, 06:56 PM   PM User | #7
Grant Palin
Regular Coder

 
Join Date: Jun 2002
Location: Victoria, BC, Canada
Posts: 962
Thanks: 0
Thanked 1 Time in 1 Post
Grant Palin is an unknown quantity at this point
Quote:
Originally Posted by _Aerospace_Eng_
Grant said it quite well.
Thank you, thank you. I'll be here all week.
Grant Palin is offline   Reply With Quote
Old 07-18-2005, 06:57 PM   PM User | #8
]|V|[agnus
Regular Coder

 
Join Date: May 2004
Location: Minneapolis, MN, USA
Posts: 904
Thanks: 0
Thanked 0 Times in 0 Posts
]|V|[agnus is an unknown quantity at this point
Yes, thank you Grant. I understood that concept before your explanation, but it was the lack of parenthesis that threw me in the original snippet.
__________________

Opposite of Sequitur
]|V|[agnus is offline   Reply With Quote
Old 07-18-2005, 08:08 PM   PM User | #9
Grant Palin
Regular Coder

 
Join Date: Jun 2002
Location: Victoria, BC, Canada
Posts: 962
Thanks: 0
Thanked 1 Time in 1 Post
Grant Palin is an unknown quantity at this point
Quote:
Originally Posted by ]|V|[agnus
Yes, thank you Grant. I understood that concept before your explanation, but it was the lack of parenthesis that threw me in the original snippet.
Those nested ternary statements can get really tricky to understand properly, and adding parentheses always help the reader!
Grant Palin is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:20 PM.


Advertisement
Log in to turn off these ads.