| Author |
how to get back bean value in javascript
|
Harini nataraj
Greenhorn
Joined: Jan 10, 2012
Posts: 10
|
|
Hi,
can anyone please help me out in getting my bean value in javascript.
|
 |
Jaik Singh
Greenhorn
Joined: Sep 22, 2010
Posts: 20
|
|
Get the bean's attribute value in any of the component, keep that component hidden and access that component in your java script.
Thanks
Jaikrat Singh
|
 |
Harini nataraj
Greenhorn
Joined: Jan 10, 2012
Posts: 10
|
|
trying to do the same but i am getting null value
<h:inputHidden id="html" value="#{ques.str}" />
function onCall()
{
var htmlstring = document.getElementById('form:html').value;
alert(htmlstring);
}
tried something like this, i m new to jsf, that would be great if someone helps
|
 |
Jaik Singh
Greenhorn
Joined: Sep 22, 2010
Posts: 20
|
|
Try once without using hidden attribute, like simple inputText. If it works, try with hidden attribute.
Thanks
Jaikrat Singh
|
 |
Tim Holloway
Saloon Keeper
Joined: Jun 25, 2001
Posts: 14456
|
|
It depends on what point in time you're looking to obtain the value from.
To get the backing bean property value as of page load time, it can be as simple as this:
Anything more immediate would require AJAX.
|
Customer surveys are for companies who didn't pay proper attention to begin with.
|
 |
Harini nataraj
Greenhorn
Joined: Jan 10, 2012
Posts: 10
|
|
|
Thank you guys, i got it.
|
 |
Aiswarya Geidth
Greenhorn
Joined: Jan 10, 2012
Posts: 13
|
|
Hi
"<f:verbatim><script>
function onCall() {
alert("#{ques.str}");
}
</script></f:verbatim> "
I used this code but i am not got the value, when alert opens it shows "#{ques.str}" this as it is.
How can you resolve this one. Please tell me.
Thanks
|
 |
Tim Holloway
Saloon Keeper
Joined: Jun 25, 2001
Posts: 14456
|
|
|
Hmmm. Try it without the "f:verbatim" tags. I didn't think that they'd disable the EL processing, but maybe they did.
|
 |
Aiswarya Geidth
Greenhorn
Joined: Jan 10, 2012
Posts: 13
|
|
|
Sorry! still it's not working.
|
 |
Kumaravadivel Subramani
Ranch Hand
Joined: Jul 05, 2008
Posts: 161
|
|
I don't feel this is a good method to do it. By enabling <f:verbatim> parsing would not happen when the page gets rendered, so EL expression would be off. (Always <script> should not be parsed as a best practice).
Try save the bean value in hidden input box, then extract by java script function call. Here you have used faces input box <h:inputHidden> with id attribute, but faces will be given different id value than what you have specified.
Create input hidden box inside 'div' or 'span' and extract them by (div.id/span.id).firstChild. This should work for you.
|
No pain, No gain.
OCJP 1.6
|
 |
Aiswarya Geidth
Greenhorn
Joined: Jan 10, 2012
Posts: 13
|
|
Can you please provide me any sample code.
Thanks
|
 |
Harini nataraj
Greenhorn
Joined: Jan 10, 2012
Posts: 10
|
|
<h:inputHidden id="html" value="#{ques.conversiontostring}" />
function onCall()
{
var htmlstring = document.getElementById("form:html").value;
alert(htmlstring);
}
actually the above code worked for me. try to do this.
|
 |
Harini nataraj
Greenhorn
Joined: Jan 10, 2012
Posts: 10
|
|
<h:commandButton value="display_alert" onclick="onCall()" />
and call that function onclick of command button or use it as per your requirement
|
 |
Aiswarya Geidth
Greenhorn
Joined: Jan 10, 2012
Posts: 13
|
|
|
I tried that one, but i am getting null value.
|
 |
Harini nataraj
Greenhorn
Joined: Jan 10, 2012
Posts: 10
|
|
In the beginning even i was getting null value. Make sure that your Bean class should look like this.
public String getConversiontostring()
{
str = "some text whatever you need in you java script";
System.out.println("My Html :" + str);
return str;
}
for the above function you have to make the call as below, ques is my bean name.
#{ques.conversiontostring}
it must work
|
 |
Aiswarya Geidth
Greenhorn
Joined: Jan 10, 2012
Posts: 13
|
|
Finally I got the solution.
This is JSF
Bean class:
Now it's working.
Thank you.
|
 |
kumar toomu
Greenhorn
Joined: Dec 29, 2011
Posts: 14
|
|
if your bean is in session scope you can use this...
alert('${beanname.property}');
|
 |
 |
|
|
subject: how to get back bean value in javascript
|
|
|