Using javascript and forms for dynamic page.

Discussion in 'JavaScript' started by sin0cide, Nov 8, 2007.

  1. #1
    I have an issue that I have been trying to understand that is eluding my basic knowledge of javascript. I have a request form that is setup to figure out what someone needs to give them custom questions. So my question is what would be an easy way to activate a function to change a select field's options in forms depending on quantity requested.

    Example:

    How many copyright forms do you need? (if more than one I need to make is so they can choose for multiple additional forms like add author add title so on)

    Thanks in advance for any information or ideas
     
    sin0cide, Nov 8, 2007 IP
  2. sin0cide

    sin0cide Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    k so I figure it out... gunna post this for remarks or if anyone else needs something like this

    
    <html>
    <head>
    <script type="text/javascript">
    <!--
    amount = 1
    
    function addOption(quan) {
    while ( amount < quan ) {
     amount = amount + 1
     x = document.getElementById("option1").innerHTML
     y = "<input type=\"checkbox\" value=\"yes\">Add Title to Copyright #" + amount
     z = x + y
     document.getElementById("option1").innerHTML = z
     }
    }
    //-->
    </script>
    </head>
    
    <body>
    <select name="">
     <option selected>1 Copyright</option>
     <option onClick="addOption(2)" >2 Copyright</option>
     <option onClick="addOption(3)" >3 Copyright</option>
    </select>
    <div id="option1">
    <input type="checkbox" value="yes">Add Title to Copyright #1
    </div>
    <br /><br />
    
    </body>
    </html>
    
    Code (markup):
     
    sin0cide, Nov 8, 2007 IP
  3. sin0cide

    sin0cide Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    So I figure out that it does not output exactly how it needs to. Becuase if you change your mind after selecting an option and try to change the option it does not overwrite the other option correctly
     
    sin0cide, Nov 9, 2007 IP
  4. JeffHood

    JeffHood Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hello.

    Here's my try on what I think you're trying to accomplish:
    <script type="text/javascript">
    <!--
    function addOption(quan){
    	var html = "";
    	for(i=1;i<=quan;i++){
    		html += "<input type=\"checkbox\" value=\"yes\" name=\"copyright" + i + "\">Add Title to Copyright #" + i + "<br />";
    	}
    	document.getElementById("option1").innerHTML = html;
    }
    //-->
    </script>
    HTML:
    It would be easy to modify the script to 'remember' the selected values before it renders the new HTML as well but I'm unsure if that's within the scope of your question.
     
    JeffHood, Nov 12, 2007 IP