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
HarryHHHarryHH 

Add a Javascript to a visualforce page

Hello,

 

I have a visualforce page for editing an order-position with two amounts (one per day and one that ist calculated). For beeing new on force.com and with javascript I tried to calculate the second ammount with a very simple operation. But it doesn't work. Can anyone help? Here is my page:

 

<apex:page standardController="Auftragsposition__c" >
<script>
function changeMgges (input)
{
    document.edit-form.edit-block.edit-section.Mgges.value = input * 10 ;
}
</script>
    <apex:sectionHeader title="{!$ObjectType.Auftragsposition__c.label}" subtitle="{!Auftragsposition__c.name}"/>
    <apex:form ID="edit-form">
    <apex:pageBlock id="edit-block" title="{!$ObjectType.Auftragsposition__c.label} Detail" mode="edit">
        <apex:pageBlockButtons >
            <apex:commandButton action="{!save}" value="Speichern"/>
            <apex:commandButton action="{!delete}" value="Löschen"/>
            <apex:commandButton action="{!URLFOR($Action.Auftragsposition__c.Clone,Auftragsposition__c.id)}" value="Duplizieren"/>
        </apex:pageBlockButtons>
        <apex:pageBlockSection ID="edit-section" showHeader="false" columns="2">
            <apex:inputField value="{!Auftragsposition__c.Name}"/>
            <apex:inputField ID="MgTg" value="{!Auftragsposition__c.Gen_Menge_Stk_Tag__c}" onchange="changeMgges(this}');"/>
            <apex:inputField ID="Mgges" value="{!Auftragsposition__c.Gen_Menge_ges__c}"/>
            <apex:inputField value="{!Auftragsposition__c.Genehmigungsstatus__c}"/>
        </apex:pageBlockSection>
   </apex:pageBlock>
   </apex:form>  
 </apex:page>

 

Thanks

Harry

Best Answer chosen by Admin (Salesforce Developers) 
WesNolte__cWesNolte__c

Small syntax issue:

 

onchange="changeMgges(this}

 

The brace in red should be ")"

 

Wes

All Answers

Pradeep_NavatarPradeep_Navatar

To refer to a Visualforce component in JavaScript or another Web-enabled language you must specify a value for the id attribute for that component. The $Component

global variable simplifies references and reduces some of the dependency on the overall page structure.

 

For example, to access a data table with id="tableID" contained in a page block with id="blockID", use the following expression: $Component.blockID.tableID.

 

For more information, visit:

http://www.salesforce.com/us/developer/docs/pages/Content/pages_best_practices_accessing_id.htm

 

Hope this helps.

HarryHHHarryHH

Thanks that helped! But now the new script:

<script>
function changeMgges (input)
{
alert('Javascript' + input);
var ele=document.getElementById('{!$Component.edit_form.edit_block.edit_section.Mgges}');
ele = ele * 10;
alert('Ele = ' + ele);                

}
</script>

 

is not invoked if I change the vaule of the Field with ID "MgTg". It should be called by this line:

 

            <apex:inputField ID="MgTg" value="{!Auftragsposition__c.Gen_Menge_Stk_Tag__c}" onchange="changeMgges(this}"/>
            <apex:inputField ID="Mgges" value="{!Auftragsposition__c.Gen_Menge_ges__c}"/>

 

Can help here also? Thanks very much!

Harry

WesNolte__cWesNolte__c

Small syntax issue:

 

onchange="changeMgges(this}

 

The brace in red should be ")"

 

Wes

This was selected as the best answer
HarryHHHarryHH

Hello Wes,

 

thanks very much. That helped. I think the idea of writing a Handbook for beginners is very good. There is a lot of documentation on force.com, but for beginners like me, there is always the question, which part of this is relevant for my actual problem and with wich one should I start to learn a new lesson.

 

Thanks and good luck for the book (will it be published when the dreamforce is started? I'm from germany and I think it will last a while until the book will be sold in germany, so it would be great if I could buy it at the dreamforce)

Harry

WesNolte__cWesNolte__c

Glad to help. I'm in the UK so it'll be available in Europe :)

 

It should be ready by Dreamforce too.

 

Wes