aspose file tools*
The moose likes JSF and the fly likes JSF and javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "JSF and javascript" Watch "JSF and javascript" New topic
Author

JSF and javascript

Awais Sheikh
Ranch Hand

Joined: Jun 07, 2006
Posts: 48
I am updating an application in JSF. I was calculating a textbox value on each keypress event using javascript in previous application. But now in JSF we give the property of bean that directly picks up the values. Now how it could b done in JSF??
Sergey Smirnov
Ranch Hand

Joined: May 29, 2003
Posts: 167
do you mean you have a calculator formula on the server side and want to use it with onkeypress on the client side?
Awais Sheikh
Ranch Hand

Joined: Jun 07, 2006
Posts: 48
Thanx a lot for your reply.

Actually i m calling a client side javascript from a html inputbox, on each key press, in my previous page. Same thing i want to do in JSF.


so please guide me how it will be done in JSF
Thanx
Sergey Smirnov
Ranch Hand

Joined: May 29, 2003
Posts: 167
As soon as JSF renders h:inputText into <input type="text"> html tag, you can do the same you did previously. I.e. use onkeypress attribute on the hnputText
Awais Sheikh
Ranch Hand

Joined: Jun 07, 2006
Posts: 48
Thanks a lot I got that. Actually I was writing onKeyPress instead of onkeypress ("all letters are in small") and it was giving me error message as "Attribute onKeyPress invalid for tag inputText according to TLD".
But here arise another problem . How to get value of a JSF inputbox in JavaScript? Previously I was getting value of a text box in javascript as below


Where empcode is the name of textbox.

Now in JSF how can I get value of JSF inputbox?? According toJSF Tags listed here; JSF inputbox has no name element. Without name how values can b accessed in javascript.
Sergey Smirnov
Ranch Hand

Joined: May 29, 2003
Posts: 167
instead of getting reference to the input field in the body of the javascript function, pass it with parameter.
I.e.

<h:inputText id="empcode" onkeypress="KeyPressed(this)" ......
....
....
function KeyPressed(ref) {
alert(ref.value);
}
[ June 30, 2006: Message edited by: Sergey Smirnov ]
Awais Sheikh
Ranch Hand

Joined: Jun 07, 2006
Posts: 48
Thanx a lot for your kind reply.
Problem has been resolved and now I can pick values from javaScript.
Here I have one thing more to discuss with u. i.e. Now after getting values in javascript I want to send that values in some second JSF textbox, i dont have parameter or reference of that textbox in javascript. How to resolve this issue??

Thanx
Nimchi Yung
Ranch Hand

Joined: Jan 27, 2004
Posts: 71
Use id attribute from your 2nd JSF textbox.

--Nimchi
Sergey Smirnov
Ranch Hand

Joined: May 29, 2003
Posts: 167
The 'name' attribute of the form element is created based on the 'id' atribute of the jsf component and the 'id' attribute of the form. For example, if you have:

The name of the elements might be "myform:foo" and "myform:bar". So, you can access them by using document.getElementById method. For example:



However, if you use any other naming container component to surround the form, such as f:subview, the additional prefix might be added to the name attribute.
To avoid this ambiguity, you can pass the name of the form as a parameter in the javascript. For example:


[ July 03, 2006: Message edited by: Sergey Smirnov ]
Arshad Sharief
Greenhorn

Joined: Feb 23, 2006
Posts: 14

Hello,
Iam trying to submit the form whenever a Enter key is pressed in the textbox by getting a ref to the af:commandbutton using document.getElementById('formName:buttonName') but this always returns null.

function test(form){

if(window.event.keyCode == 13){
document.getElementById(form.name +':buttonName').click();
}
}

And I also tried with document.getElementById(form.name +'::buttonName') but this also returns null.

Is it ':' or '::'

Thanks.
Arshad Sharief
Greenhorn

Joined: Feb 23, 2006
Posts: 14

Oops.. missed the tags

inputText keypress="return test(this.form)"

commandButton id="buttonName"
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSF and javascript