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
devloper sfdcdevloper sfdc 

I want to calculate Total Price column using java script but my code not working

Hi All

I want to calculate Total Price Column Automaticaly when enter price and quanty  using java script . I also written code but it is not working. 
<apex:page standardController="Opportunity" extensions="ProductEntry" >
   <script>
 
        function show(myId){
            var rowIds=myId.split(':');
            var price=document.getElementById('page:fm:pb:pbt:'+rowIds[4]+':price').value;
            var quant=document.getElementById('page:fm:pb:pbt:'+rowIds[4]+':quant').value;
            var total=price*quant;
            document.getElementById('page:fm:pb:pbt:'+rowIds[4]+':total').value=total;
          document.getElementById('page:fm:hid').value=rowIds[4];
        }
   
        
   
    
    </script>
    
    <apex:form id="fm" >
         <apex:inputHidden value="{!rowNo}" id="hid" />
        <apex:pageBlock title="Selected Products" id="pb">
            
            
            <apex:pageBlockTable value="{!Optyline}" var="opt" id="pbt">
             <apex:column value="{!opt.Product2Id}" headerValue="Product"/>
                <apex:column headerValue="List Price" >
                <apex:inputText value="{!opt.UnitPrice}" id="price" onchange="show(this.id);" />
                </apex:column>
                <apex:column headerValue="Quantity" >
                    <apex:inputText value="{!opt.Quantity}" id="quant" onchange="show(this.id);"  html-placeholder="Enter quantity" required="true"/>
                </apex:column>
                <apex:column headerValue="Total Price" >
                    <apex:inputText id="Total"/>
                </apex:column>
                       
               
         
                
                 </apex:pageBlockTable>
           
           <apex:pageBlockButtons >
               <apex:commandButton value="save" action="{!addtOpp}"/>
               <apex:commandButton value="Confirm selection " action="{!hideSection}"/>
            </apex:pageBlockButtons>
            
        </apex:pageBlock>
        <apex:pageblock title="Step 1- Select Product Bundle" rendered="{!flag1}" >
              <apex:pageBlockSection columns="1" >
               
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Bundle Type"/>
                    <apex:selectList size="1" value="{!selectedname}"> 
        <apex:selectOptions value="{!selectedaccnamefields}"/> 
                       
                        <apex:actionSupport event="onchange" action="{!getData}" reRender="pb"/>
                        
    </apex:selectList>
                     
                </apex:pageBlockSectionItem>
                </apex:pageBlockSection>
            <apex:pageBlockTable value="{!wrappers}" var="a" id="pb">
                   <apex:column headerValue="Selected">
                       <apex:inputCheckbox value="{!a.isSelected}" id="Chk"/>
                    <apex:actionSupport event="Onclick" action="{!addlist}" reRender="pb"/>   
                </apex:column>
                   <apex:column value="{!a.prod.Name}" headerValue="Product"/>
                <apex:column value="{!a.prod.UnitPrice}" headerValue="List Price"/>
               
               
         
                
                 </apex:pageBlockTable>
             <apex:pageBlockButtons >
                
                <apex:commandButton value="Add Product(s)" action="{!AddtoOpty}" reRender="fm" oncomplete="alertCall();"/>
             
                
            </apex:pageBlockButtons>
            
        </apex:pageblock>
    </apex:form>
</apex:page>
devloper sfdcdevloper sfdc
please help me
 
Narender Singh(Nads)Narender Singh(Nads)
Hi,
The reason your javascript code is not giving the expected results is because the rows are getting created dynamically, and therefore, that function doesn't get bind with those rows. I can't help you with get entire code but I can definitely point you in the right direction.

In order to get the desired functionality, you will have to use Jquery in your code.
Simply import the jquery library in your script.
Now, you have to use '.on()' function.

Refer to this link:  https://stackoverflow.com/questions/13047169/click-event-doesnt-fire-for-table-rows-added-dynamically?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

This .on() function is your solution here. I haven't much worked much with JS and Jquery otherwise I would have helped you with the code as well.

Let me know if you need any further help.

Regards,
Narender