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
NehaKSNehaKS 

How to get apex page message in javascript

<apex:page standardController="Account">
<script type="text/javascript">
function test()
{

var accname=document.getElementById("{!$Component.f.pb1.pbs1.a}").value;
alert(accname);
var message=document.getElementById("{!$Component.f.pb1.msg}").value;
alert(message);

}

</script>

<apex:form id="f">
<apex:pageBlock id="pb1">
<apex:pageMessages id="msg" />
<apex:pageBlockSection id="pbs1">
<apex:inputField value="{!account.name}" id="a" />
<apex:inputField value="{!account.industry}"/>
<apex:commandButton action="{!save}" value="Save!"/>
<apex:commandButton onclick="test();" value="test"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

 For the above code

I am getting the value of field in account name but i am not able to get the error message displayed in alert

Its giving as "undefined".

 

Could anyone please help me out with this.

Best Answer chosen by Admin (Salesforce Developers) 
tukmoltukmol

pageMessages is not a web form element (i.e. button, textbox) so it does not have "value" (undefined).

 

try

 

document.getElementById("{!$Component.f.pb1.msg}").textContent;

 

or

 

document.getElementById("{!$Component.f.pb1.msg}").innerText;

 

whichever works.

All Answers

tukmoltukmol

pageMessages is not a web form element (i.e. button, textbox) so it does not have "value" (undefined).

 

try

 

document.getElementById("{!$Component.f.pb1.msg}").textContent;

 

or

 

document.getElementById("{!$Component.f.pb1.msg}").innerText;

 

whichever works.

This was selected as the best answer
NehaKSNehaKS
document.getElementById("{!$Component.f.pb1.msg}")​.textContent;

 

 This worked. Thanks a lot :)

 

What if i want to get the value of a column in pageblock table using javascript. Is it possible using the value attribute.

I used the .value function but it returns nothing not even null.

 

Could you help me out with this issue please

 

NehaKSNehaKS
<apex:page controller="AddRows" tabstyle="Account">  
<script type="text/javascript">
function test()
{
var message=document.getElementById("{!$Component.f.pb1.wtable.i1}").value;
alert(message);
var message1=document.getElementById("{!$Component.f.pb1.wtable.i}").value;
alert(message1);

}

</script>
<apex:form id="f">    
<apex:pageBlock title="Bulk Account Create" id="pb1">
<apex:pageMessages id="msg"/>
<apex:pageBlockTable value="{!wrappers}" var="wrapper" id="wtable">          
<apex:column headerValue="Ident" id="i">             
<apex:outputText value="{!wrapper.ident}" id="i1"/>          
</apex:column>          
<apex:column headerValue="Name">             
<apex:inputField value="{!wrapper.acc.Name}"/>          
</apex:column>          
<apex:column headerValue="Parent">             
<apex:inputField value="{!wrapper.acc.ParentId}"/>          
</apex:column>          
<apex:column headerValue="Industry">             
<apex:inputField value="{!wrapper.acc.Industry}"/>         
</apex:column>         
<apex:column headerValue="Type">             
<apex:inputField value="{!wrapper.acc.Type}"/>          
</apex:column>          
<apex:column headerValue="Action">             
<apex:commandButton value="Delete" action="{!delWrapper}" rerender="wtable">             
<apex:param name="toDelIdent" value="{!wrapper.ident}" assignTo="{!toDelIdent}"/>              
</apex:commandButton>          
</apex:column>       
</apex:pageBlockTable>       
<apex:commandButton value="Add Row" action="{!addRows}" rerender="wtable">         
<apex:param name="addCount" value="1" assignTo="{!addCount}"/>        
</apex:commandButton>       
<apex:commandButton value="Add 5 Rows" action="{!addRows}" rerender="wtable">          
<apex:param name="addCount" value="5" assignTo="{!addCount}"/>        
</apex:commandButton>       
<apex:commandButton value="Save" action="{!save}"/>  
<apex:commandButton value="Test" onclick="test();"/>
</apex:pageBlock> 
</apex:form> 
</apex:page>

 Apex Class:-

 

public class AddRows  
{  
    public List<AccountWrapper> wrappers {get; set;}  
    public static Integer toDelIdent {get; set;}  
    public static Integer addCount {get; set;}  
    private Integer nextIdent=0;     
    public Addrows() 
     {   wrappers=new List<AccountWrapper>();   
         for (Integer idx=0; idx<5; idx++)   
         {    
            wrappers.add(new AccountWrapper(nextIdent++));   
         }  
      }     
      public void delWrapper()  
      {   
           Integer toDelPos=-1;   
           for (Integer idx=0; idx<wrappers.size(); idx++)   
           {    
                if (wrappers[idx].ident==toDelIdent)    
                {    
                     toDelPos=idx;    
                }   
           }       
            if (-1!=toDelPos)   
            {    
              wrappers.remove(toDelPos);   
            }  
       }     
       public void addRows() 
        {   
          for (Integer idx=0; idx<addCount; idx++)   
          {    
               wrappers.add(new AccountWrapper(nextIdent++));   
           }  
        }     
        public PageReference save()  
        {   
               List<Account> accs=new List<Account>();   
               for (AccountWrapper wrap : wrappers)   
               {    
                     accs.add(wrap.acc);   
               }       
               insert accs;       
               return new PageReference('/' + Schema.getGlobalDescribe().get('Account').getDescribe().getKeyPrefix() + '/o');  
         }    
         public class AccountWrapper 
         {   
          public Account acc {get; private set;}   
          public Integer ident {get; private set;}       
          public AccountWrapper(Integer inIdent)   
               {    
             ident=inIdent;    
             acc=new Account(Name='abc');   
               } 
           } 
}

 Refer this code:

 I want to display the column value...the focus should be on the row selected 

NehaKSNehaKS

Please help ..Its on urgent basis