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
bblaybblay 

Javascript in VF Page

I'm trying to add the currently logged in user to a text field of mine which then displays a list of other users who have clicked the 'Add Name' button on a particular custom object of mine for events (Date__c). 

 

My javascript below works perfectly how I would like it to work meaning it adds the user's name to the next line in a long text area field after the user clicks the button.

 

 

<apex:form> 
<textarea name="textarea1">{!Date__c.Date_List__c}</textarea><br/> 
<input type="button" name="insert" value="Add Name" onclick="this.form.textarea1.value=this.form.textarea1.value.concat('\r\n{!$User.communitynickname}');"/>  
</apex:form>

 

 

My issue though is when I try to convert the html textarea into an <apex:inputtextarea> tag so I can then save the info. I tried the below code and it looks exactly the same on the VF page as the above code but it doesn't add the user name to the inputtextarea field.

 

 

<apex:form> 
<apex:inputTextarea id="textarea1" value="{!Date__c.Date_List__c}"/><br/>
<input type="button" name="insert" value="Add Name" onclick="this.form.textarea1.value=this.form.textarea1.value.concat('\r\n{!$User.communitynickname}');"/>  
</apex:form> 

 

 

Any thoughts on this? I have a feeling it's something simple but I'm just not sure. Thanks for any ideas or suggestions!

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Pradeep_NavatarPradeep_Navatar

You miss javascript code to assign value to input field.

 

Code below will work for you :

 

<apex:page standardController="Account" showHeader="false"  sidebar="false">

<apex:form>

<apex:inputtextarea id="textarea1" value="{!account.description}"/><br/>

<input type="button" value="Add Name" onclick="document.getElementById('{!$Component.textarea1}').value=document.getElementById('{!$Component.textarea1}').value.concat('\r\n{!$User.communitynickname}');"/> 

</apex:form>   </apex:page>

All Answers

sinsiterrulezsinsiterrulez

Hi,

Is the path of textarea added in JS correct as when we have apex component we basically access them using $Component... thing..

bblaybblay

hmm interesting point! This doesn't work, but I'm not too terribly sure how to make the correct path for this. There were no errors and the output still looks the same, just doesn't add the nickname to the text box....

 

 

<apex:form id="form1"> 
<apex:inputtextarea id="textarea1" value="{!Date__c.Date_List__c}"/><br/>
<input type="button" name="insert" value="Add Name" 
onclick="this.{!$Component.form1.textarea1.value}=this.{!$Component.form1.textarea1.value}.concat('\r\n{!$User.c ommunitynickname}');"/>  
</apex:form>

 

 

 

Pradeep_NavatarPradeep_Navatar

You miss javascript code to assign value to input field.

 

Code below will work for you :

 

<apex:page standardController="Account" showHeader="false"  sidebar="false">

<apex:form>

<apex:inputtextarea id="textarea1" value="{!account.description}"/><br/>

<input type="button" value="Add Name" onclick="document.getElementById('{!$Component.textarea1}').value=document.getElementById('{!$Component.textarea1}').value.concat('\r\n{!$User.communitynickname}');"/> 

</apex:form>   </apex:page>

This was selected as the best answer
bblaybblay

Thank you so much! This worked perfectly!