Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSF and javascript

 
Awais Sheikh
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use id attribute from your 2nd JSF textbox.

--Nimchi
 
Sergey Smirnov
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops.. missed the tags

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

commandButton id="buttonName"
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic