0

I use jsf and primefaces components and I want to set a value to an h:outputText throught jquery

I inspect the tag with chrome tool (the id is the same)

but the value doesn't be set to the element here is the jsf code :

 <p:confirmDialog id="confirmDialog" message="Etes vous sur de vouloir supprimer ce Type #{typeMB.selectedType.libelle}"  
                             header="confirmation de suppression" severity="alert" widgetVar="confirmation"> 
                             <h:outputText id="fortest" value="donc" /> 

<h:form>
                <p:commandButton id="confirm" value="oui" update=":form:ourdatatable" oncomplete="confirmation.hide()"  actionListener="#{typeMB.supprimer}" />  
                <p:commandButton id="decline" value="non" onclick="confirmation.hide()" type="button" />   
</h:form>
            </p:confirmDialog> 

here is the html code for my h:outputText tag :

<span id="fortest">donc</span>

and here is the jquery script :

<script type="text/javascript">  


 $(function() {      




     $('#form\\:ourdatatable\\:0\\:alors').click(function() {
          alert("I am here");
          var classList =$('#form\\:ourdatatable\\:0\\:alors').attr('class').split(/\s+/);
          $.each( classList, function(index, item){
              if(index > 1){
                  alert("I am here here ");
                  $('#fortest').text('here here');

              }

              });
        });


 });

</script> 

I tested .text() and val() and html() method but the value is not set

thank you in advance

atbegin-but
  • 263
  • 1
  • 8
  • 19
  • Does this answer your question? [How can I know the id of a JSF component so I can use in Javascript](https://stackoverflow.com/questions/6045307/how-can-i-know-the-id-of-a-jsf-component-so-i-can-use-in-javascript) – Kukeltje Jan 12 '20 at 17:50

3 Answers3

1

The problem is as follows: there is two callbacks function for the click event, yours is very faster then the one of primefaces since it uses ajax, so when your function executes the markup is not ready yet, so it does not do what you expect!

The solution: you need to sequentially chain your callbacks (executes your function after the primefaces callback finishes). To implement this you can use promise to observe the ConfirmDialog (pay attention this will not work if you observe the wrong element).

Hidalgo
  • 765
  • 1
  • 9
  • 28
-2

Try to add attribut prependId="false" for each form

Mohamed AbdElRazek
  • 1,654
  • 14
  • 17
-2

It should be $('#fortest').val('here here') when you set the value not $('#fortest').text('here here'). Cause even it's rendering as span, JSF component takes the value. And add prependId="false" to your form.

SRy
  • 2,901
  • 8
  • 36
  • 57
  • I tested val() and html() but no result, I also added prependId="false" but no, the strang thing is when I execute this jquery instruction in console of the navigator it works – atbegin-but Mar 15 '13 at 16:51
  • Then I guess you didn't include the jquery library properly into the page.Can you show how you included `Jquery` library in your page?Did you include `` in your page?Atleast are you able to see the `alert` on the page? – SRy Mar 15 '13 at 17:55
  • the alert appears and the value is set when I use the console of the navigator when the confirmdialog is opened but when the dialog is not open : even if I change the value with the console it is not affected because the jsf value is set after the value of jquery(it ovveride it) : that is what I understood – atbegin-but Mar 16 '13 at 06:06
  • now I need to set the value after the opening of confirmdialog (to ovveriding the value set by jsf) this is the problem (I don't know how) – atbegin-but Mar 16 '13 at 06:08
  • https://stackoverflow.com/questions/7415230/uiform-with-prependid-false-breaks-fajax-render and this is all plain not related to jsf using 'value' – Kukeltje Jan 11 '20 at 09:05