function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
WEN JIEWEN JIE 

A simple question about apex:inputField

Hi,

 

I want to know how to get value about this tab on Visualforce page.

 

 

Thank you!

Best Answer chosen by Admin (Salesforce Developers) 
mast0rmast0r

Do it like this:

 

<apex:inputField value="{!First_Name__c}" id="FirstName" />

<script type="text/javascript">
    var jq$ = jQuery.noConflict();

    var firstName = jq$('[id$=FirstName]').val();

    alert(firstName);
</script>

 

All Answers

mast0rmast0r

What value do you mean? Id, input-value, what tab?

Chamil MadusankaChamil Madusanka

Please elaborate your question.

WEN JIEWEN JIE

Hi,

 

I have some <apex:inputField> tags on my Visualforce page, when I do the save operation, I want to get the value about these input values and check these values.

I use document.getElementById, but can't get the value.

 

Thank you!

mast0rmast0r

Try to use jQuery, it is very usefull:

 

<apex:inputField value="{!someVar}" id="myInputFieldId" />


<script>
    var theValue = jQuery('[id$=myInputFieldId]').val();
</script>

 

WEN JIEWEN JIE

Hi,

 

I have some <apex:inputField> tags on my Visualforce page, when I do the save operation, I want to get the value about these input values and check these values.

 

Thank you!

mast0rmast0r

Huh?

WEN JIEWEN JIE

Hi,

 

Have a look with my code:

<apex:inputField value="{!First_Name__c}" id="FirstName" />

 

<script type="text/javascript">
var jq$ = jQuery.noConflict();

var firstName = jq$('FirstName').val();

alert(firstName );

</script>

 

But can't get the value. I have upload the jquery file in static resource and invoke them in my page.

 

mast0rmast0r

Do it like this:

 

<apex:inputField value="{!First_Name__c}" id="FirstName" />

<script type="text/javascript">
    var jq$ = jQuery.noConflict();

    var firstName = jq$('[id$=FirstName]').val();

    alert(firstName);
</script>

 

This was selected as the best answer
WEN JIEWEN JIE

Hi ,

 

I saw the error about "jQuery undefined" on my page. But I have include the js file in my page.

 

<apex:includeScript value="{!URLFOR($Resource.JQuery, 'JQuery/js/jquery-ui-1.7.3.custom.min.js')}"/>
<apex:includeScript value="{!URLFOR($Resource.JQuery, 'JQuery/js/jquery-1.3.2.min.js')}"/>

<apex:includeScript value="{!URLFOR($Resource.JQuery, 'JQuery/js/jquery.validate.min.js')}"/>
      
<apex:stylesheet value="{!URLFOR($Resource.JQuery, 'JQuery/css/ui-lightness/jquery-ui-1.7.3.custom.css')}"/>
mast0rmast0r

Remove JQuery from your path:

 

<apex:includeScript value="{!URLFOR($Resource.JQuery, 'js/jquery-ui-1.7.3.custom.min.js')}"/>
<apex:includeScript value="{!URLFOR($Resource.JQuery, 'js/jquery-1.3.2.min.js')}"/>
<apex:includeScript value="{!URLFOR($Resource.JQuery, 'js/jquery.validate.min.js')}"/>
<apex:stylesheet value="{!URLFOR($Resource.JQuery, 'css/ui-lightness/jquery-ui-1.7.3.custom.css')}"/>

 

tk.servicetk.service

Are the id attributes defined in your apex tags? Use them directly in a hierachical manner with document.getElementById().

 

Example:

<apex:Page id="myPage">

   <apex:PageBlock id="myPB">

      <apex:PageBlockItem id="myPBI">

         <apex:inputField id="myInput">

         </apex:inputField>

      </apex:PageBlockItem>

   </apex:PageBlock>

</apex:Page>

 

Get the value in javascript, no jQuery required:

document.getElementById('myPage:myPB:myPBI:myInput').value

 

Check and confirm this by looking at the page source and see what id that salesforce apex has generated for the inputField. If you see in the id 'j_xxx' it means no id defined and salesforce generated the id. In above example, if I didn't define id for myPage, i may have to use document.getElementById('j_id0:myPB:myPBI:myInput').value

 

If it's an iterated component, for example used in a PageBlockDataTable, salesforce will generate the ids with iteration number included:

xxx:yyy:zzz:0:myInput

xxx:yyy:zzz:1:myInput

xxx:yyy:zzz:2:myInput

...

 

Hope this helps. Further reading: $Component and it's usage.

WEN JIEWEN JIE

Hi,

 

I remove the JQuery with my path, then it works, thank you!

WEN JIEWEN JIE

Hi,

 

I have get the value by qjuery, but your method also useful for me. 

Thank you!