wood burning stoves*
The moose likes JSF and the fly likes JSF form: using ENTER as TABULATOR key Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "JSF form: using ENTER as TABULATOR key" Watch "JSF form: using ENTER as TABULATOR key" New topic
Author

JSF form: using ENTER as TABULATOR key

Joe Shannow
Ranch Hand

Joined: Dec 01, 2010
Posts: 35
Hi there, I'm newbie in jsf and need your help.

I have a h:form with several h:inputTexts and submit button. Pressing enter during filling a form causes the same action as submit button. But i need cursor to go to the next input field like using TAB key. I'm only able to disable enter key using a <h:form onkeypress="return event.keyCode != 13;"> expression.

Do you have some idea? Javascript or some jsf function?
Joe Shannow
Ranch Hand

Joined: Dec 01, 2010
Posts: 35
Nobody? I've found this working solution (IE only):
<h:form onkeydown="javascript:if(window.event.keyCode == 13) window.event.keyCode = 9;">

but now, I need to use ENTER normally on submit button. I don't know why, but this solution:
onkeydown="javascript:if(window.event.keyCode == 13 && window.event.srcElement.id !='btnSubmit') window.event.keyCode = 9;

throws javax.servlet.ServletException: null source

some suggestions?
Joe Shannow
Ranch Hand

Joined: Dec 01, 2010
Posts: 35
Problem is in &&, "and" works normally. But now, it's submitted whenever I press ENTER.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15960
    
  19

You're better off using "and", anyway. But your problem is in XML entities. Because the View is defined as an XML document, the 5 magic characters (& ", ', <, > usually need to be coded. So instead of "&&", you'd have to code "&amp;&amp;". It's much easier to just say "and".

And you don't even want to know what I had to do to make the above display correctly! The CodeRanch edit/display feature has many of the same restrictions!


Customer surveys are for companies who didn't pay proper attention to begin with.
Joe Shannow
Ranch Hand

Joined: Dec 01, 2010
Posts: 35
Tim Holloway wrote:And you don't even want to know what I had to do to make the above display correctly! The CodeRanch edit/display feature has many of the same restrictions!

Thanks Tim, but I don´t understand.. display correctly what?


However, this method works completely (included in .js file):

function enter2tab(){
if(window.event.keyCode == 13 && window.event.srcElement.id !='scrap:btnSubmit')
window.event.keyCode = 9;
}


<h:form id="scrap" onkeydown="enter2tab();">
<h:commandButton value="save" type="submit" id="btnSubmit" action="#{myScrap.saveNew}"/>
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15960
    
  19

I had to use XML entities on the "magic" characters when editing that message to ensure that they would display properly.

However, your latest is mixing apples and oranges, I think.

If you put the javascript in a javascript file (apple), the XML rules don't apply to that file. In fact, they won't work. You have to say "&&".

But JavaScript in an XML file (xhtml file, JSF View source. orange) does have to follow XML rules. Otherwise, it wouldn't be XML! So you usually have to do &amp;&amp; or just code "and". Although I don't think "and" works in JavaScript, just in EL.
Joe Shannow
Ranch Hand

Joined: Dec 01, 2010
Posts: 35
Still don't understand.. "and" (and maybe &_amp;&_amp; - aha here's the first restriction :-) works when I call it directly from jsf tag using onkeydown or onclick attributes.
But in the js file only && works. I've found only one solution which I've posted before....
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15960
    
  19

Well, the name of the game is to create JavaScript. If the javascript is in a JavaScript file, then it's already javascript and therefore usable "as is".

But a JSF xhtml View is an XML file. And, like I said, XML has a constraint on the "5 magic characters", because if you coded them directly, the XML parsers wouldn't be able to tell XML markup from data within the markup. For example:


Question: is the first double-quote the string terminator, or is the second double-quote the string terminator? OK, so that's easy to tell. For you. The CodeRaqnch syntax colorer obviously doesn't thing it's so simple. What if you have a whole FILE full of those puppies? Stack enough of them together and they start being readable in multiple (ambiguous) ways. So the proper usage is via the XML double-quote entity:


That way, no guesswork is involved. Since the Ampersand is the escape character, the Ampersand itself must be escaped, so that you don't end up trying to puzzle out stuff like the following:


And so forth.

Now, in some cases, the parser will cut you some slack. For example, you can usually use an apostrophe "as is" within quotes without having to resort to its XML entity form. But if there's a chance of confusion, you won't be allowed that. And it's never wrong to do it anyway.

Since "&" is the escape character, you're pretty much required to use its entity form at all times.

As for the use of the word "and" instead of "&&", like I said, I don't believe that "and" is valid javascript. The confusion there is that "and" is valid EL, and if you don't pay attention to context, you can confuse EL with javascript, since they're both java-like languages.

In JSF, however, the giveaway is that EL is delimited using the "#{}" construct.

What makes this even MORE fun is that you can have EL embedded within JavaScript! And I have, in fact. Basically, EL becomes a "macro language" at that point, and the results of evaluating the EL subexpression(s) replaces the EL to form an ordinary text string, which is then interpreted as JavaScript.

And no, "a #{&&} b" isn't legal. The double-ampersand is going to be interpreted as a binary operator, and there's no left and right terms of the expression that it would be applied to. "a" and "b" are outside the EL context.

Joe Shannow
Ranch Hand

Joined: Dec 01, 2010
Posts: 35
Aha, I've got it. You're talking about differencies between XML based views and JavaScript character representing.
If I'm right, my javascript function is ok now and you told me why doesn´t work my previous post. && in jsf.

Thanks for your patience, If I'm not wrong, now I understand what are we talking about
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSF form: using ENTER as TABULATOR key
 
Similar Threads
HTML +Javascript submit query??
the enter key forces a submit
Submitting a form on pressing Enter key
selectOneMenu value not being set on postback
2 buttons listening for the same key