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
Stefaan Somers 11Stefaan Somers 11 

how to use javascript variable in visual force page

I have a visualforce page that uses a tag from a packaged component:
<cxsrec:getMapValue map="{!candidateFieldMap}" key="Specialty__c"/>

I want to do something that 
If Specialty = 'Test1' then show 
<div> Test 1 </div>
If  Specialty = 'Test2' then show
<div> Test 2 </div>

I can try to obtain the value maybe through jquery or something, but how can I reuse this variable then afterwards in my visualforce page.
 
SubratSubrat (Salesforce Developers) 
Hello Stefaan ,

You can use a Visualforce expression to retrieve the value of Specialty__c, and then use it to conditionally render the div elements. Here's an example:
<apex:outputPanel id="specialtyPanel">
  <cxsrec:getMapValue map="{!candidateFieldMap}" key="Specialty__c"/>
</apex:outputPanel>

<script>
  $(document).ready(function() {
    var specialty = $('#specialtyPanel').text().trim();
    if (specialty === 'Test1') {
      $('<div>Test 1</div>').appendTo('#outputPanel');
    } else if (specialty === 'Test2') {
      $('<div>Test 2</div>').appendTo('#outputPanel');
    }
  });
</script>

<apex:outputPanel id="outputPanel">
  <!-- Output the div elements here based on the condition -->
</apex:outputPanel>

This code first wraps the <cxsrec:getMapValue> tag in an <apex:outputPanel>, which allows you to retrieve its text content using jQuery.

If the above information helps , please mark this as Best Answer.
Thank you.