SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Member Wizcrafts's Avatar
    Join Date
    Sep 2003
    Location
    Flint, Michigan, USA
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Need help with Select Options fill Textarea with selected messages

    Greeting the forum;

    I am having trouble wit ha Javascript function that interacts with a form's select box and textarea. The selectbox has a number of options to choose from, with the default selection telling the visitor to choose from the flyout list. The textarea has a default readonly message upon page loading. The script I have, which is typed below, will only change the textarea to the first option, called "Proposal A" no matter what other choices I make in the select box. I want it to change to any and all options and input then into the textarea for the user to make his/her final choice before submitting the form (it is a voting type of form).

    Here is my current code for the select box on the form page:
    Code:
                     <select name="Proposals" onchange="loadComments()">
                      <option selected>Choose a Proposal here</option>
    
                      <option>Proposal A </option>
    
                      <option>Proposal B </option>
    
                      <option>Proposal C </option>
    
                      <option>Proposal D </option>
                    </select>
    Here is the Javascript code from an external js file:
    Code:
    function loadComments()
    {
      //var f1 = document.form.Proposals.selectedIndex
      var f1 = document.form.Proposals[document.form.Proposals.selectedIndex].value
      var f2 = document.form.Comments
      var txt1 = "Testing the script for Proposal A"
      var txt2 = "Testing the script for Proposal B"
      var txt3 = "Testing the script for Proposal C"
      var txt4 = "Testing the script for Proposal D"
    
      if (f1 = 1) 
      {
        f2.value = txt1
      }
    
      else if (f1 = 2)
      {
        f2.value = txt2
      }
    
      else if (f1 = 3)
      {
        f2.value = txt3
      }
    
      else if (f1 = 4)
      {
        f2.value = txt4
      }  
    }
    Any help will be appreciated. Thanks in advance.

  2. #2
    SitePoint Addict xDev's Avatar
    Join Date
    Jul 2003
    Location
    Moncton, New Brunswick, Canada
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    The problem is your conditionals. Your assigning a variable instead of comparing. use == instead of =

    Code:
      if (f1 == 1) 
      {
        f2.value = txt1;
      }
      else if (f1 == 2)
      {
        f2.value = txt2;
      }
      else if (f1 == 3)
      {
        f2.value ==txt3;
      }
      else if (f1 == 4)
      {
        f2.value = txt4;
      }
    Better yet, use a case structure since your only comparing the value of f1 anyway:

    Code:
    switch(f1)
    {
      case 1:
      f2.value = txt1;
      break;
      case 2:
      f2.value = txt2;
      break;
      case 3:
      f2.value = txt3;
      break;
      case 4:
      f2.value = txt4;
      break;
    }
    Also you could of used an array for txt and saved some typing and space:

    var txt1 = "Testing the script for Proposal A"
    var txt2 = "Testing the script for Proposal B"
    var txt3 = "Testing the script for Proposal C"
    var txt4 = "Testing the script for Proposal D"
    Code:
    var txt = [
    "Testing the script for Proposal A",
    "Testing the script for Proposal B",
    "Testing the script for Proposal C",
    "Testing the script for Proposal D"
    ];
    
    switch(f1)
    {
      case 1:
      f2.value = txt[0];
      break;
      case 2:
      f2.value = txt[1];
      break;
      case 3:
      f2.value = txt[2];
      break;
      case 4:
      f2.value = txt[3];
      break;
    }
    -xDev

  3. #3
    SitePoint Member Wizcrafts's Avatar
    Join Date
    Sep 2003
    Location
    Flint, Michigan, USA
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks XDev, that did the trick.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •