function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SFDC_LearnerSFDC_Learner 

Need Help with Jquery

<apex:page id="p1">
    <apex:includeScript value="{!$Resource.jQueryFile}"/>
 
    <input type="button" value="welcome" id="txt1"/> 
    <apex:outputText value="hello, good morning" id="txt2" rendered="false"></apex:outputText>
    
     <script>
     $(document).ready(function() {
        test();
     });
 
     $('#txt1').click(function() {
         // alert(document.getElementById('txt2').value);
         alert('hello');
      });
       
           
     </script>
</apex:page>
 
 
 
Q : Why this statement is showing : Undefined ( // alert(document.getElementById('txt2').value);)
amarcuteamarcute

replace alert(document.getElementById('txt2').value);

with

alert(document.getElementById('{!$Component.txt2}').value);

 

Also, remove rendered="false". With this, the element will never get rendered in the resulting HTML. You can make it hidden if you dont want to display it. But, it should get rendered on the page.

Avidev9Avidev9

Well when you have imported the awesome JQUERY library, why not to use that to get the value.

 

The catch is that the ids generate are lil different in VF. They are generally prefixed with some automated generated texts. So you will need the ends with selector to get the value.

 

Try this

 

alert($( "[id$='txt2']" ).val( ));

SFDC_LearnerSFDC_Learner

Showing null only .. :)

Avidev9Avidev9
$( "[id$='txt2']" ) use this selector for click function and as well as for grabbing the value