In general, I figured out the dynamic list, now the problem is the following. ) I need to enter the text that will be attached to the opions when selecting options in textarea. For example, there is a form

<form name="form1" action="" method="POST"> <select class=select id="brands_id" name="myselect" onchange="catalog_rebuild(this.options[this.selectedIndex].value)"><option value="0">--- <option value="1">Toyota <option value="2">Nissan </select> <textarea name="mytext">Какой-Ρ‚ΠΎ тСкст!</textarea> </form> 

With the help of which script can I get, say, when choosing the item Toyota to display the text in the textarea with the description of this company?

    3 answers 3

    Here's how to do it.

    HTML:

     <form> <select id='select'> <option>---</option> <option>Nissan</option> <option>Toyota</option> </select> <br/> <textarea id='textarea1'></textarea> <br/> <textarea id='textarea2'></textarea> <div id='description1' style='display: none;'>nissan</div> <div id='description2' style='display: none;'>toyota</div> </form> 

    Javascript / jquery:

     /* No jquery */ function onchange1(e) { if (this.selectedIndex == 1) { document.getElementById('textarea1').value = 'nissan'; } if (this.selectedIndex == 2) { document.getElementById('textarea1').value = 'toyota'; } } document.getElementById('select').onchange = onchange1; /* JQuery */ function onchange2(e) { if (this.selectedIndex == 1) { $('#textarea2').text('nissan'); } if (this.selectedIndex == 2) { $('#textarea2').text('toyota'); } } $('#select').bind('change', onchange2); $('#select').bind('change', function(){ $('[id^=description]').hide(); $('#description' + this.selectedIndex).show(); }); 

    And in detail, the option tag must have a closing <option></option> .

    And it is not clear why you use output in textarea, if this is only a description that the user needs as a hint or something like that.

    Under the link I made some examples.

       <script type="text/javascript"> var descr = new Array(); descr[0] = 'Π’Ρ‹Π±Π΅Ρ€ΠΈΡ‚Π΅ ΠΌΠ°ΡˆΠΈΠ½Ρƒ'; descr[1] = 'ОписаниС Π’ΠΎΠΉΠΎΡ‚Ρ‹'; descr[2] = 'ОписаниС Ниссана'; function catalog_rebuild(x) { document.getElementById("mytext").value = descr[x]; } </script> <form name="form1" action="" method="POST"> <select class=select id="brands_id" name="myselect" onchange="catalog_rebuild(this.value)"> <option value="0">---</option> <option value="1">Toyota</option> <option value="2">Nissan</option> </select> <textarea name="mytext" id="mytext">Какой-Ρ‚ΠΎ тСкст!</textarea> </form> 

      Although I also prefer the non-textarea option:

       <script type="text/javascript"> var descr = new Array(); descr[0] = 'Π’Ρ‹Π±Π΅Ρ€ΠΈΡ‚Π΅ ΠΌΠ°ΡˆΠΈΠ½Ρƒ'; descr[1] = 'ОписаниС Π’ΠΎΠΉΠΎΡ‚Ρ‹'; descr[2] = 'ОписаниС Ниссана'; function catalog_rebuild(x) { document.getElementById("mytext").innerHTML = descr[x]; } </script> <form name="form1" action="" method="POST"> <select class=select id="brands_id" name="myselect" onchange="catalog_rebuild(this.value)"> <option value="0">---</option> <option value="1">Toyota</option> <option value="2">Nissan</option> </select> <div id="mytext">Какой-Ρ‚ΠΎ тСкст!</div> </form> 
      • I tried the option with the div does not work says "line 2 character 1 - the presence of the object is assumed " - Respayn
      • Sorry, really my mistake. Too used to php. =) Corrected. - ling

      I tried the option with the div does not work writes that "line 2 character 1 - the presence of the object is assumed"