4 Replies Latest reply on Mar 18, 2008 12:14 PM by lasansue.jeremy.girard.satives.fr

    Javascript problem retrieving element

    lasansue.jeremy.girard.satives.fr

      Hi all,


      I need to change using javascript the content of an input value.
      But seam add some ramdom j_idxxx in the input id.


      How can i do to retrieve the element  ?



      Thanks.

        • 1. Re: Javascript problem retrieving element

          it's a JSF thing
          you could add id's to all your JSF naming containers (h:form, etc.): then the id would not change dynamically, or you could use smth. like the j4j:idProxy

          • 2. Re: Javascript problem retrieving element
            keithnaas

            Depending on what you are doing you could also probably make use of rich:jquery or rich:componentControl

            • 3. Re: Javascript problem retrieving element
              dipas

              You have multiple possibilities


              If you are using a4j on your page then you can use prototype.js (rendered by richfaces) - in the newest richfaces release prototype.js 1.6 is used.



              <h:form id="myForm">
              ....
              <h:inputText id="myControl" .....
              
              </h:form>



              is rendered like that or something similar ...



              <form id="myForm">
               .... 
              <input id="myForm:j145:myControl"
              </form>



              you can find that control with the following javascript



              <script>
                var cometome = $('myForm').select('input[id$=:myControl]'); // matches the end
                alert(cometome);
                var cometome2 = $('myForm').select('input[id^=myControl:]'); // matches the beginning
                alert(cometome2);
                var cometome3 = $('myForm').select('input[id*=:myControl:]'); // matches any part of the value
                alert(cometome3);
              </stript



              or you put a cssclass on your input



              <h:form id="myForm">
               .... 
              <h:inputText styleClass="findMe someOtherClass" ...
              



              and find it by doing this


              <script>
               var cometome = $('myForm').getElementsByClassName('findMe');
               // you receive an array
               alert(cometome[0]);
              </script>



              • 4. Re: Javascript problem retrieving element
                lasansue.jeremy.girard.satives.fr

                Hi,


                Thank u all for your replies.


                I can't make j4j prowy with xhtml. The generated html does not
                contains <span ... as decribe in the doc.


                I used di pas solution. It works fine but there is something to change.


                var cometome is an array. To get your input field you need to add [0]



                <script>
                  var cometome = $('myForm').select('input[id$=:myControl]')[0]; // matches the end
                  alert(cometome.value); 
                </stript>



                That's it. Hope it helps.