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
SurjenduKSurjenduK 

document.getElementById is not working for me in visualforce page? Help!!!

Code:
<apex:page controller="SamePageController" tabstyle="Lead" standardStylesheets="false" >
<script type="text/javascript">
function grabExName()
{
var ex = document.getElementById("exname").value;
alert(ex);
}
</script>


<apex:sectionHeader title="Create Extranets">
<apex:form>

<apex:pageBlock>

<apex:pageBlockSection columns="3" title="Extranet Information">

<apex:outputLabel style="font-weight:bold;" value="Extranet Name" for="exname"/>
<apex:inputText id="exname" required="true" onkeydown="grabExName()"></apex:inputText

</apex:pageBlockSection>

</apex:pageBlock>
</apex:form>
</apex:sectionHeader>

</apex:page>

 What am I doing wrong here? I get an error which says document.getElementById("exname") does not exist. How do i refer the inputText id i created?

Best Answer chosen by Admin (Salesforce Developers) 
Ron HessRon Hess
Code:
<apex:page controller="SamePageController" tabstyle="Lead" standardStylesheets="false" >

<apex:sectionHeader title="Create Extranets">
<apex:form>

<apex:pageBlock>

<apex:pageBlockSection columns="3" title="Extranet Information">

<apex:outputLabel style="font-weight:bold;" value="Extranet Name" for="exname"/>
<apex:inputText id="exname" required="true" onkeydown="grabExName()">

<script type="text/javascript">
function grabExName()
{
var ex = document.getElementById('{!$Component.exname}').value;
alert(ex);
}
</script>
</apex:inputText>

</apex:pageBlockSection>

</apex:pageBlock>
</apex:form>
</apex:sectionHeader>

</apex:page>



try this


Message Edited by Ron Hess on 04-07-2008 08:28 PM

All Answers

SurjenduKSurjenduK
after doing some search in the forum I tried the following

Code:
<script type="text/javascript">
function grabExName()
{
var ex = document.getElementById('{!$Component.exname}');
alert(ex);
}
</script>

 and the script tag is within the <apex:pageBlock>. Still it does not work.

Ron HessRon Hess
the expression

{!$Component.exname}

should be a child of the component that you are extracting the name from

Ron HessRon Hess
Code:
<apex:page controller="SamePageController" tabstyle="Lead" standardStylesheets="false" >

<apex:sectionHeader title="Create Extranets">
<apex:form>

<apex:pageBlock>

<apex:pageBlockSection columns="3" title="Extranet Information">

<apex:outputLabel style="font-weight:bold;" value="Extranet Name" for="exname"/>
<apex:inputText id="exname" required="true" onkeydown="grabExName()">

<script type="text/javascript">
function grabExName()
{
var ex = document.getElementById('{!$Component.exname}').value;
alert(ex);
}
</script>
</apex:inputText>

</apex:pageBlockSection>

</apex:pageBlock>
</apex:form>
</apex:sectionHeader>

</apex:page>



try this


Message Edited by Ron Hess on 04-07-2008 08:28 PM
This was selected as the best answer
LinThawLinThaw
Hi, SurjenduK and all

Have you got solution for <apex:pageBlock> ?
and the script tag is within the <apex:pageBlock>. Still it does not work.

I want to design pageBlock and pageBlockSection such as background color etc.

Is there anyway to use style?

Regards,
LinThaw.
karthik subramanian 13karthik subramanian 13

<apex:page  tabstyle="Lead" standardStylesheets="false" >
<script type="text/javascript">
function grabExName(myid)
{
    
    var ex = document.getElementById(myid).value;
    alert(ex);
}
</script>


<apex:sectionHeader title="Create Extranets">
<apex:form>

<apex:pageBlock>

<apex:pageBlockSection columns="3" title="Extranet Information">

<apex:outputLabel style="font-weight:bold;" value="Extranet Name" for="exname"/>
<apex:inputText id="exname" required="true"  onkeydown="grabExName(this.id)"></apex:inputText>

</apex:pageBlockSection>

</apex:pageBlock>
</apex:form>
</apex:sectionHeader>

</apex:page>


try tis one

this.id is required and you can get it by myid in script

karthik subramanian 13karthik subramanian 13
when you try doing dyamic things,then "Id" has be passed dynamically not the standard one like "exname", which you have used