Javascript Not Working

I wish to have some result like the link HERE- javascript. But, it doesn’t seems to work. Is there any mistakes in my code??




<script type="text/javascript">

	var mytextbox = document.getElementById('mytext');

	var mydropdown = document.getElementById('dropdown');

	

	mydropdown.onchange=function()

	{

	  mytextbox.value=mytextbox.value+this.value;

	}

	</script>


	

	<div style="margin-left:70px">

    <?php echo $form->labelEx($model,'lecturer'); ?>

	<div style="margin-left:110px; margin-top:-30px">

	<?php echo $form->dropDownList($model,'lecturer', CHtml::listData(Lecturer::model()->findAll(), 'lecturer_name', 'lecturer_name'), array(

	'empty'=>'Please Select Lecturer',

	'id'=>'dropdown'

	))?>

    </div></div><br>

	

	<div style="margin-left:70px">

    <?php echo $form->labelEx($model,'lecturer2'); ?>

	<div style="margin-left:110px; margin-top:-30px">

	<?php echo $form->textArea($model,'lecturer2', array('maxlength'=>100, 'id'=>'mytext'))?>

    </div></div><br>



I think you should use the option "onchange" in the dropdownlist

PHP




array('onchange'=>'changemydropdown(this);')



JS




function changemydropdown(variable){

   var demo = variable.value;

   alert(demo);

   ...

}



Sorry for the code that is not finished but we can help you

Hi,joel.campusano! Thank you so much for your reply. I can have the value displayed on the text area after i select the dropdownlist but I wish to have more values displayed whenever i select dropdownlist more than once just like the link here Javascript.




<script type="text/javascript">

	var mytextbox = document.getElementById('mytext');

	var mydropdown = document.getElementById('dropdown');

function choice()

	{

	var mytextbox = document.getElementById('mytext');

	var mydropdown = document.getElementById('dropdown');

	

		mytextbox.value = mydropdown.value;

		

	}

	

	</script>


	

	<div style="margin-left:70px">

    <?php echo $form->labelEx($model,'lecturer'); ?>

	<div style="margin-left:110px; margin-top:-30px">

	<?php echo $form->dropDownList($model,'lecturer', CHtml::listData(Lecturer::model()->findAll(), 'lecturer_name', 'lecturer_name'), array(

	'empty'=>'Please Select Lecturer',

	'id'=>'dropdown',

	'onchange'=>'{choice();}'

	))?>

    </div></div><br>

	

	<div style="margin-left:70px">

    <?php echo $form->labelEx($model,'lecturer2'); ?>

	<div style="margin-left:110px; margin-top:-30px">

	<?php echo $form->textArea($model,'lecturer2', array('maxlength'=>100, 'id'=>'mytext'))?>

    </div></div><br>



This code can only work for selecting dropdownlist for once. How to improve this code if I wan to select dropdownlist more than one time and all the values are displayed on the text area??

beginner123:

You must first obtain the value found in the textarea.




var content = $("#mytext").val();



Second, you must print the value of the textarea concatenated with the text of the dropdownlist.




$("#mytext").val(content + demo);



Regards.

getElementById will return null. You should move the javascript to the end of the file and make sure it’s run after the form is created.

I’m sorry that I still can’t get the result that I want like the example here [color="#FF0000"]Javascript[/color]. Can you guide me in a complete code??

Thanks in advance.