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
tsalb_tsalb_ 

Pass value from inside a repeat to javascript function - client side

Is there a client-side way of passing a value from within an apex:repeat to a javascript function?

 

Basically, my list is iterated, with each unique itemLat / itemLon, and I'd need to pass the specific lat/lon back to a javascript function for some processing. Any ideas on how to approach this?

 

The reason I'd want to do this is because I have a separate JS function that does some JS remoting, so ideally, i'd like to just pass the new params to that function so it can do it's work in the controller, but with the new lat/lon as the starting parameters.

 

   <apex:repeat var="frm" id="searchResult" value="{!newSearchList}">
     <div id="search-item">
       <p style="font-size:12px">
         <apex:outputLink value="javascript&colon;void(0)" 
onclick="setCoords(itemLat, itemLon);" id="itemLink"> <b>{!frm.Customer_gne__r.Name}</b> <apex:param name="itemLat" value="{!frm.Test_Lat__c}"/> <apex:param name="itemLon" value="{!frm.Test_Lon__c}"/> </apex:outputLink><br/> {!frm.Primary_Address_gne__r.Name}, {!frm.Primary_Address_gne__r.City_vod__c}<br/> {!frm.Primary_Address_gne__r.Phone_vod__c}<br/> </p> </div> </apex:repeat>
//Snippet function setCoords(passedLat,passedLon) { alert("testing the alert!" + passedLat + " " + passedLon); }

 

Best Answer chosen by Admin (Salesforce Developers) 
tsalb_tsalb_
<apex:outputLink value="javascript&colon;void(0)" 
                 onclick="getControllerData({!frm.Test_Lat__c},{!frm.Test_Lon__c},'All',100,10)" 
                 id="itemLink">
   <b>{!frm.Customer_gne__r.Name}</b>
</apex:outputLink>

 My knowledge of javascript really failed here, got some simple advice. This is the simplest way to do it, as it will iterate over each onclick function just like any other element - and it will always pass the "correct" value to the function like this:

 

function (passedLat, passedLon) {
  alert(passedLat + " | " + passedLon);
  //do stuff
}

 

All Answers

JHayes SDJHayes SD

You could try using a <div> element with the style attribute set to display:none.  Something like this:

 

 <apex:repeat var="frm" id="searchResult" value="{!newSearchList}">
     <div id="search-item">
     <div id="testLat" style="display:none">{!frm.Test_Lat__c}</div>
     <div id="testLon" style="display:none">{!frm.Test_Lon__c}</div>
       <p style="font-size:12px">
         <apex:outputLink value="javascript&colon;void(0)" 
                          onclick="setCoords(itemLat, itemLon);" id="itemLink">
         <b>{!frm.Customer_gne__r.Name}</b>
           <apex:param name="itemLat" value="{!frm.Test_Lat__c}"/>
           <apex:param name="itemLon" value="{!frm.Test_Lon__c}"/>
         </apex:outputLink><br/>
         {!frm.Primary_Address_gne__r.Name}, {!frm.Primary_Address_gne__r.City_vod__c}<br/>
         {!frm.Primary_Address_gne__r.Phone_vod__c}<br/>
       </p>
     </div>
   </apex:repeat>

 From here you can use something like jQuery to pull back all of the <div> elements with ids testLat or testLon.  If you need to keep track of each latitude/longitude pair you can wrap those in a <div> as well.

 

Hope this helps!

-- Jeremy

tsalb_tsalb_
<apex:outputLink value="javascript&colon;void(0)" 
                 onclick="getControllerData({!frm.Test_Lat__c},{!frm.Test_Lon__c},'All',100,10)" 
                 id="itemLink">
   <b>{!frm.Customer_gne__r.Name}</b>
</apex:outputLink>

 My knowledge of javascript really failed here, got some simple advice. This is the simplest way to do it, as it will iterate over each onclick function just like any other element - and it will always pass the "correct" value to the function like this:

 

function (passedLat, passedLon) {
  alert(passedLat + " | " + passedLon);
  //do stuff
}

 

This was selected as the best answer