You need to sign in to do that
Don't have an account?
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!
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
Hi,
Is the path of textarea added in JS correct as when we have apex component we basically access them using $Component... thing..
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....
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>
Thank you so much! This worked perfectly!