+ Start a Discussion
Sudipta DebSudipta Deb 

Refer VisualForce IDs in jQuery

Hi Team-

I would like to refer VisualForce Ids in jQuery using partial ID selector. Below is the code -
<apex:page id="page">
    <apex:form id="form">
        What is your name?
        <br/>
        <apex:inputText id="name" onfocus="myFunc()"/>
        <br/>
        What is your age?
        <br/>
        <apex:inputText id="ageText" onfocus="myFunc()"/>
    </apex:form>
    <apex:includeScript value="{!URLFOR($Resource.jQuery1_11, '/js/jquery-1.11.2.min.js')}"/>
    <script>
        function myFunc() {
            var j$ = jQuery.noConflict();
            var inputTextNameCatched = j$( 'input[id$=name]' ); //Find element with ID ending with name
            inputTextNameCatched.val('Sudipta Deb');
            var inputTextAgeCatched = j$('input[id$=Text]');
            inputTextAgeCatched.val('30');
        }
    </script>
</apex:page>
The above code is working perfectly fine. But when I am trying to get IDs starts with "age" with the below code - 
var inputTextAgeCatched = j$('input[id^=age]');
it is not working. Can anyone please help me to understand what is the problem? Thanks in advance.
Best Answer chosen by Sudipta Deb
Vishnu VaishnavVishnu Vaishnav

Hi Sudipta ,

You can try this:

var inputTextAgeCatched = j$('input[id*=age]');
 

All Answers

Vishnu VaishnavVishnu Vaishnav
Hi Sudipta ,

You can try this:
var inputTextAgeCatched = j$('input[id$=ageText]');
                              or
var inputTextAgeCatched = j$('input[id*=ageText]');
Sudipta DebSudipta Deb
Hi Vishnu,

Thanks for your answer. id$ will work for sure. But what about if I want to refer VisualForce IDs starting with "age" (Say for example - I only know that ID will start with "age", nothing else). I was trying to use id^, but it was not working. Any idea why it was not working?
Grazitti TeamGrazitti Team

Hi Sudipta,

Try the following syntax, it works for me.
var inputTextAgeCatched = j$('[id^="age"]');

Please Mark this as Answer if it helps you
--
Regards,
Grazitti Team
Web: www.grazitti.com
Email: sfdc@grazitti.com
Vishnu VaishnavVishnu Vaishnav

Hi Sudipta ,

You can try this:

var inputTextAgeCatched = j$('input[id*=age]');
 
This was selected as the best answer
Sudipta DebSudipta Deb
Hi Vishu - 

Yes, it is working now. Thanks for your help. I am going to mark your reply as answer.