I have loaded some info in a h:outputText
but now I want to click a button and trigger a JS function which will take that text from the outputText.
The structure is:
<p:tabView id="tabView">
<p:tab id="tabInvitator" title="Invitator">
<h:form id="formInvitator">
<h:outputText id="inv" value="#{myController.selectedUsersFB}" />
...
But in the JS function document.getElementsById("tabView:tabInvitator:formInvitator:inv") doesn't work. It reinits the web app.
I just want to get that data and manipulate it in my JS function.
Any suggestions?
I've tried with what BalusC suggested, here is my try:
function test(){
var arr = document.getElementsByName("tabView:tabInvitator:formInvitator:inv");
console.log(arr);
console.log('Arr: '+arr);
console.log('Length: '+arr.length);
The first log returns: [ ], The second one returns: Arr: [object NodeList], The thid one: Length: 0
Why does the log return different things? And now, how can I access to that object NodeList if that's suppose a text from h:outputText?
First of all, it's a unique ID what I want to update so, the JS call would be: document.getElementById(...) (and not in plural -getElementsById-) since I will just return the data from an which contain a value, normally text.
And following my example, if I want to update firstly the outputText and after that, get that info in the JS function I can do it in a row, first updating the field (this field I put it out of any tags), and oncomplete this, call the JS function:
<p:tabView id="tabView">
<p:tab id="tabInvitator" title="Invitator">
<h:form id="formInvitator">
<p:commandButton value="Invite" icon="ui-icon-comment" update=":inv" oncomplete="sendRequestViaMultiFriendSelectorSelected();"/>
</h:form>
</p:tab>
</p:tabView>
<h:outputText id="inv" value="#{myController.selectedUsersFB}" />
and the Javascript function:
function sendRequestViaMultiFriendSelectorSelected() {
var arr = document.getElementById("inv");
console.log(arr.childNodes[0]);
if(arr.childNodes[0]){
console.log(arr.childNodes[0].data);
}
...
}
Hope it helps someone ;)