12

I'm trying something simple, just to get the text value inside a p:inputText thought Javascript, but I`m not getting it. Perhaps it's a different procedure with Primefaces, since if I don't use that, I don't encounter the problem.

My code:

<p:inputText value="any text" widgetVar="youtlink" ></p:inputText>
        <p:commandButton value="Search"    onclick="loadPlayer();" icon="ui-icon-search" />  


<script type="text/javascript">
    function loadPlayer() {
        alert(youtlink.text);
    }
</script>

I have tried also with JQuery but also no success.

Rendered view:

<form id="editlFrm" enctype="application/x-www-form-urlencoded" method="post"
name="editlFrm">
    <input id="editlFrm:j_id_7" class="ui-inputfield ui-inputtext ui-widget ui-state-default ui-corner-all"
    type="text" value="assd" name="editlFrm:j_id_7" role="textbox" aria-disabled="false"
    aria-readonly="false" aria-multiline="false">
    <button id="editlFrm:j_id_8" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-left"
    type="submit" onclick="loadPlayer();;PrimeFaces.ab({source:'editlFrm:j_id_8'});return false;"
    name="editlFrm:j_id_8" role="button" aria-disabled="false">
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
cri_sys
  • 472
  • 1
  • 5
  • 17

4 Answers4

12

Add id to your <p:inputText

like this

<p:inputText id="someID" value="any text" widgetVar="youtlink" ></p:inputText>

make sure your form got prependId="false"

than access the value like this

alert(jQuery('#someID').val());

if you don't want to add prependId="false" to your form you will have to change the jquery selector from jQuery('#someID').val() to jQuery("[id$='someID']").val()


EDIT

since your form called editlFrm

try this (make sure to assign someID id to your p:inputText)

alert(jQuery('#editlFrm\\:someID').val());
Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Daniel
  • 36,833
  • 10
  • 119
  • 200
9

I came across this question when trying to do something similar, and discovered that PrimeFaces exposes a widget's JQuery element through the jq property.

So in your example, you could simply do:

function loadPlayer() {
    alert(youtlink.jq.val());
}

In more recent PrimeFaces versions (since 5.0), exported widgets seem to no longer pollute the global namespace, but need to be accessed using the PF() method. The JQuery element can however still be accessed in the same way.

function loadPlayer() {
    alert(PF('youtlink').jq.val());
}
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
7

My I think the simplest way:

PF('youtlink').jq.val();

You can set the value of input field like this:

PF('youtlink').jq.val('new value');

other way:

PrimeFaces.widgets.youtlink.jq.val()

and another way if you have ID:

$(PrimeFaces.escapeClientId("yourFormID:youtlink")).val()

tested with PF 5.3

Michal.S
  • 501
  • 6
  • 12
0

Also, there a simple way with javascript:

<h:form id="yourID" >
    <p:inputText id="anyID" value="any text" widgetVar="youtlink" ></p:inputText>
</h:form>

function loadPlayer() {
    alert(document.getElementById('yourID:anyID').value);
}
Edson Cezar
  • 25,884
  • 5
  • 19
  • 27