0

In the controller, I specify the PageReference src as http...../apex/mypage?id=' + {!a.Id} and for href as "!URLFOR("apex/mypage?id=" + a.Id)} in the tag. But in jQuery:

$('#Link').click(function(){
    $("#iframe").attr("src", "/apex/mypage?id=" ??? );
}

To specify an URL in jQuery, how do I GET the Salesforce id in the URL. (Not the element Id but the 15 character "key" in SF)

2

If your javascript is in your Visualforce page you can simply use the same binding:

$('#Link').click(function(){
    $("#iframe").attr("src", "/apex/mypage?id={!a.Id}");
}

Assuming that a is available to the page (which it would appear to be so from the question).

If the code is not stored in the page directly, you could add a variable to the page:

<script type="text/javascript">
  var accountId = "{!a.Id}";
</script>

And just use the variable accountId in the code you're bringing in from elsewhere (note the check to see if it's actually defined):

$('#Link').click(function(){
    var pageUrl = "/apex/mypage?id=" + (accountId ? accountId : "");
    $("#iframe").attr("src", pageUrl);
}

Another alternative would be to put the value into a hidden field somewhere:

<apex:inputHidden value="{!a.Id}" id="theId"/>

and then use jQuery to pull that input's value inside your click function.

  • Thanks for the brief answer @LaceySnr♦. But these ways are for the standard objects, if I'm right. What if I want to get the id of a wrapper class parameter (for example, if I've a COMMON_CITY parameter that contains the cities of Leads and Contacts). I was able to get the Ids only in the Pageblocktable. So how can I pull those Ids in JS? – Seesh May 30 '14 at 6:47
  • You'd need to check the format of the parameter but I suspect it'd be a semicolon delimited list. In that case you should be able to assign the value to a javascript variable (2nd block of code in my answer) then use the javascript .split string method to get an array. Then you should be able to index into that, but if you're still stuck a new question might be the way to go. – Matt Lacey Jun 2 '14 at 0:41
  • Thank you @LaceySnr♦. But I'm still stuck up and I've posted a new question in detail with the complete code. salesforce.stackexchange.com/questions/38430/… – Seesh Jun 2 '14 at 1:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.