Question

Hi i have this javascript code that when i click the qualities you want on the select dropdown list it will display to the textarea. Now i got this error TypeError: d.textarea1 is undefined d.textarea1.value = sChoices;

Is there something wrong in my code?

<!DOCTYPE html>
<html>
<head>
  <title>List Box 4 Example</title>
  <script type="text/javascript">
    function tellMe(d){
      var sChoices ="";
      for(i=0; i<d.listbox.options.length; i++){
        if(d.listbox.options[i].selected == true){
          sChoices += d.listbox.options[i].text +"\n";
        }
      }
      d.textarea1.value = sChoices;
    }
  </script>
</head>
<body bgcolor="lightgreen">
  <form name="form1">
    <p>Girl's quaalities you want?</p>
    <select name="listbox" onchange="tellMe(this.form)" multiple>
      <option>Pretty</option>
      <option>Sexy</option>
      <option>Hot</option>
      <option>Intelligent</option>
      <option>Funny</option>
    </select>
  </form>
  <br />
  You Choose:<br />
  <textarea rows="4" cols="20" name="textarea1"></textarea>
</body>
</html>

Any help is muchly appreciated! thanks

Était-ce utile?

La solution

The textarea is not in the form therefore you will not be able to reference it from the form elements, just add it to the form.

  <form name="form1">
    <p>Girl's quaalities you want?</p>
    <select name="listbox" onchange="tellMe(this.form)" multiple>
      <option>Pretty</option>
      <option>Sexy</option>
      <option>Hot</option>
      <option>Intelligent</option>
      <option>Funny</option>
    </select>
  <br />
  You Choose:<br />
  <textarea rows="4" cols="20" name="textarea1"></textarea>  
</form>

http://jsfiddle.net/TgV83/

Autres conseils

you should wrap textarea also inside the form otherwise this d.textarea1 is not known for your js code because d is your form and textarea is outside of your form. for the working code, see Musa's code.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top