Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get value text from p:inputText javascript

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">
like image 222
cri_sys Avatar asked Jul 19 '12 10:07

cri_sys


People also ask

How to get the value of input text in JavaScript?

We can get the value of the text input field using various methods in JavaScript. There is a text value property that can set and return the value of the value attribute of a text field. Also, we can use the jquery val() method inside the script to get or set the value of the text input field.

How can get input tag value in jQuery?

jQuery val() method is used to get the value of an element. This function is used to set or return the value. Return value gives the value attribute of the first element.


1 Answers

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());
like image 155
Daniel Avatar answered Sep 29 '22 04:09

Daniel