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
alexander yurpolskyalexander yurpolsky 

JavaScript in Visualforce Page

Hi. I am new to salesforce. I have to perform a simple task. When clicking an apex command button , it should alert a message through JavaScript with value from textbox "id=name" Please see my code. 
But I am not getting expected response ("null"). Please help.
<apex:page standardController="Campaign">
<apex:sectionHeader title="Campaign" subtitle="Campaign Edit"/>

<script>
function kk(){
    var lol = document.getElementById('{!$Component.cam.title.bloc.theForm.name}');
    alert(lol);
}
</script>

           <apex:form id="theForm">
           <apex:pageBlock title="Campaign" mode="edit" id="title">
           <apex:pageBlockButtons >
           <apex:commandButton onclick="kk();" value="TEST" />
                <apex:commandButton action="{!save}" value="Save"/>
                <apex:commandButton action="{!cancel}" value="Cancel"/>
                <apex:commandButton value="Save & New" action="{!'save & new'}"/>
            </apex:pageBlockButtons>
           <apex:pageBlockSection title="My Content Section" columns="2" id="bloc">
           <apex:inputfield value="{!Campaign.Campaign_Type__c}">   
                    <apex:actionSupport event="onchange" reRender="theForm" />
                 </apex:inputField>
                 <apex:inputtext value="{!Campaign.PPC__c}" rendered="{!Campaign.Campaign_Type__c == 'Campaign Type 1'}" />
                    <apex:inputText value="{!Campaign.name}" id="name" required="true" onchange="alertSample();"/> 
                 </apex:pageBlockSection>
       </apex:pageBlock>
    </apex:form>
</apex:page>

 
Alain CabonAlain Cabon
var lol = document.getElementById('{!$Component.theForm.title.bloc.name}');

<apex:form id="theForm"> 
   <apex:pageBlock title="Campaign" mode="edit" id="title">
        <apex:pageBlockSection title="My Content Section" columns="2" id="bloc">
               <apex:inputText value="{!Campaign.name}" id="name" required="true"onchange="alertSample();"/>

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_variables_global_component.htm
 
alexander yurpolskyalexander yurpolsky
Hey,

thank you for your response. I fixed the code. But now I get another message:
[object HTMLInputElement]
 
function kk(){
    var lol = document.getElementById('{!$Component.theform.title.bloc.name}');
    alert(lol);
}





 
Alain CabonAlain Cabon
function kk(){
    var lol = document.getElementById('{!$Component.theform.title.bloc.name}').value;
    alert(lol);
}

The previous message is correct but you want the value (so you must ask for it).