Adding text in textarea onclick event

Hi Experts,

I am looking into somebody else's code here and I am wondering what this line does in javascript func:

 var text = $(id).value;

So far this doesn't work.

				<td class="alignRight" style="font-weight:bold;">General:</td>
             <h:inputTextarea id="general-physicalei" value="#{self.formFields.general}" cols="50" rows="8"
              onkeyup="updateCount('#{rich:clientId('general-physicalei')}','modal-physicalexam-form:general-physicalei:notes-count','{CHAR} of 4000 characters remaining',4000);"
              <span id="modal-physicalexam-form:general-physicalei:notes-count" class="hint">Max. 4000 characters remaining</span>
	        	<s:div rendered="true"><script>
              updateCount('#{rich:clientId('general-physicalei')}','modal-physicalexam-form:general-physicalei:notes-count','{CHAR} of 4000 characters remaining',4000);
		<div id="modal-physicalexam-form:general-physicalei:default">
    <li><a href="#" onclick="addDefault('modal-physicalexam-form:general-physicalei', 'Well developed'); return false;">Add</a> Well developed</li>    
    <li><a href="#" onclick="addDefault('modal-physicalexam-form:general-physicalei',  'Well Nourished'); return false;">Add</a> Well Nourished</li>    	              	 	


function addDefault(id, defaultValue) {

   var text = $(id).value;

   if(text == '') {
      text = defaultValue;
   } else if (text.indexOf(defaultValue) == -1){
      text += ((text.charAt(text.length-1) == ' ')?'':' ')+defaultValue;

  $(id).value = text;
   //trigger update so we could limit characters
   var eventFunction =  $(id)["onkeyup"];
   if(typeof(eventFunction) == "function") {

Open in new window

Who is Participating?
ulysseyConnect With a Mentor Commented:
I think it s an id generated by the server, the id of the textarea tag is : "j_id5861:j_id3864:modal-physicalexam-form:general-physicalei"

Doesn t matter what is written inside the cote. Everything between the " " represent the id.
it s not a javascript function. It s a function made by the owner of the code or from a library like jquery but I don't know from which library this command is from.

to add text in text area you can use this :
document.getElementById("Id").setAttribute("value", "yourtext");
Id: the id of your textarea
The above code, I believe, should not have Java Server Page zone as the above code is java script code and is using either Dojo or MooTools framework or using custom java script library

Please see the below example that talks about different framework:

dkim18Author Commented:

I checked generated html and id for the textarea is like this:

<textarea id="j_id5861:j_id3864:modal-physicalexam-form:general-physicalei" name="j_id5861:j_id3864:modal-physicalexam-form:general-physicalei" cols="50" onkeyup="updateCount('j_id5861:j_id3864:modal-physicalexam-form:general-physicalei','modal-physicalexam-form:general-physicalei:notes-count','{CHAR} of 4000 characters remaining',4000);" rows="8"></textarea>

why there is "j_id5861:j_id3864" attached?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.