Search code examples
jqueryjsf-2richfaces

jsf2.0 richfaces 4.0 Jquery how to get inputfield value


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>

Solution

  • 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: