0

I am using JSF2.0 with Richfaces 4.x i am trying to get input myfield value using jquery ?

<h:outputScript name="jsf.js" library="javax.faces" />
    <h:outputScript name="jquery.js" />

    <h:outputScript target="head">
    $ = jQuery;
    // Then you can use it
    $(document).ready(function() {
    });
</h:outputScript>

<h:inputText id="myfield" value="#{dataTableBean.name}" label="Name Field">

<input type="button" onclick="javascript:myfun()" value="click me" />



<h:outputScript target="head">
    function myfun(){
    alert($('#myfield').val());
    }
 </h:outputScript>
d-man
  • 57,473
  • 85
  • 212
  • 296

1 Answers1

1

JSF runs in webserver and generates HTML. Webserver sends it to webbrowser. JavaScript runs in webbrowser and all it sees/understands is HTML only.

Open the JSF page in browser. Rightclick and View Source. Locate the HTML <input type="text"> element which is generated by JSF <h:inputText>. Look at its id attribute. It'll look something like this

<input type="text" id="formid:myfield" />

You need to use exactly that ID in the JavaScript.

alert($('#formid\\:myfield').val());

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • i don't have form name and some times i am not using form element. – d-man May 29 '12 at 18:33
  • Just give the `` an `id`. And in cases when you don't use it, just don't specify the prefix then. Again, look at the generated HTML output. You need to base your JavaScript/code on the HTML source, not on the JSF source. – BalusC May 29 '12 at 18:35