[SalesForce] Find all elements ID starting with or Ending with using JQuery in Visualforce

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 
});

Best Answer

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.
});
Related Topic