+ Start a Discussion
Rahul456Rahul456 

This JavascriptCode isn't working:

Hi All,

       Plz help me out,why this javascript(colred in red) isn't working.

<apex:page controller="abc" id="UseCaseDisplay">
<apex:form >

<apex:pageBlock  mode="edit" id="thePageBlock">
<apex:pageBlockSection columns="3">
<table width="500px" border="0" cellpadding="0" cellspacing="0">
<tr>
<td  align="left"><input type="radio" name="select" checked="true"  onClick='recentres(1)'/>Country</td>
<td  align="center"><b>OR</b></td>                   
<td  align="left"><input type="radio" name="select" onClick='recentres(2)'/>Other countries</td>
</tr>
</table>  
</apex:pageBlockSection>
</apex:pageBlock>
<br></br>

<div id="selc" style="display:block;">
<apex:pageBlock title="State Selection">

<apex:panelGrid columns="2">
Country &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<apex:selectList value="{!category}" size="1" id="category">
<apex:selectOptions value="{!categories}"/>
<apex:actionSupport event="onchange" rerender="features"/>
</apex:selectList>

<apex:outputLabel value="State" for="features"/>
<apex:selectList value="{!feature}" size="1" id="features" disabled="{!ISNULL(category)}">
<apex:selectOptions value="{!features}"/>
</apex:selectList>
</apex:panelGrid>
</apex:pageBlock>
</div>

<div id="select" style="display:none;">
<apex:pageBlock title="Others Selection" >
Country*
<apex:inputText id="cntries5" value="{!contry}">
</apex:inputText><br></br>
<br></br>
State*<apex:inputText id="state5" value="{!stat}">
</apex:inputText>
</apex:pageBlock>
</div>
<apex:commandButton value="Save Reseller Account" action="{!save}"/>
</apex:form>
 
<script language="JavaScript">
function recentres(rad)
{

if(rad==1)
{

document.getElementById("selc").style.display='block';
document.getElementById("select").style.display='none';
document.getElementById("{!$Component.state5}").value ='';
document.getElementById("{!$Component.cntries5}").value ='';

}
else
{

document.getElementById("selc").style.display='none';
document.getElementById("select").style.display='block';
}

}

</script>
</apex:page>
dchasmandchasman
Scoping issue - $Component is very sensitive to position in the page markup - it can only see things that are in the component hierarchy above or next to it (see the docs for more info) - basically things should work if you move the declaration of recentres() inside the apex:pageBlock that contains the 2 apex:inputFields things should work.
Rahul456Rahul456
Hi,

     I tried in the suggested way,it's not working:

    document.getElementById('{!$Component.cntries5}').value ='';
    Is the actual Javascript Code in my page editor

    When I checked in the error console,error showing at this line is:

        document.getElementById(' ').value ='';-->Blank value id of that Input Text.

  Plz Suggest me to proceed from this point.