3

I am trying to search all input elements that starts with a partcular set of characters such as 'idAcc' where my VF page has two inputfields with id = idAccFN and id= idAccLN respectively.

 <apex:inputField id="idAccFN" value="{!cr.FirstName__c}" />
 <apex:inputField id="idAccLN" value="{!cr.LastName__c}" />

I'm using the below JQuery syntax but thats working partially ... Explained in comments below .. Kindly help.

 var j$ = jQuery.noConflict();
 j$(document).ready(function(){
    jQuery( 'input[id$=Name]' ).val('Foo');  // ID ending with Name working 
    jQuery( 'input[id^=idAcc]' ).val('Apu')  //Id starting with idAcc not working 
});

closed as off-topic by Vigneshwaran G, Chris Duncombe, crmprogdev, Himanshu, cropredy May 28 '16 at 19:25

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions not specific to Salesforce are off-topic here, but can be asked on Stack Overflow." – Vigneshwaran G, Chris Duncombe, crmprogdev, Himanshu, cropredy
If this question can be reworded to fit the rules in the help center, please edit the question.

4

the Id you set gets prepended by VF, so you need to do a "contains" selector. And if you want each element on the page, you need to use a ".each", like this:

j$(document).ready(function(){
jQuery( 'input[id*=Name]' ).each(function(el){
  el.val('Foo'); // do something with the input here.
});
  • Thanks Jason..This serves my purpose. wondering in JQuery site contains selector is represented with '~' status. Now, got the * working instead. – CRM Guy May 21 '16 at 4:51
2

If you inspect the elements on the page you will notice that the id's are actually prepended by VF with j_id0 etc (j_id0:j_id1:page:messageDetail)if you do not place IDs on all parent elements in your vf page. Thus the ID's do not start with the idAcc that you expect.

If you need the 'Starts With' then you could look at $Component in javascript:

Use the $Component global variable to simplify referencing the DOM ID that is generated for a Visualforce component, and reduce some of the dependency on the overall page structure.

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_access.htm

  • Thanks but somehwhere I got this syntax $('[id$=theAccFN]') working instead of using $Component. S, was assuming if there is someting that can get rid of using the $Component hierarchies. – CRM Guy May 21 '16 at 3:31
  • The only way an input field would start with the ID is if you used <input> instead of the VF tags. Or you could use "contains" selector but that presents its own problems. – Eric May 21 '16 at 3:37

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