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();
    jQuery( 'input[id$=Name]' ).val('Foo');  // ID ending with Name working 
    jQuery( 'input[id^=idAcc]' ).val('Apu')  //Id starting with idAcc not working 

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:

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

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.


  • 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.