helpt to swap form onChange

Discussion in 'JavaScript' started by Abh, May 23, 2010.

  1. #1
    Hi, and sorry if if the title is not very relevant.

    I have two contact forms and a SELECT box with two option. I need it to work like this: first option = first form, second option = second form.

    So, with my close to zero knowledge of javascript i figure the second form has to be diplay:none and somehow call it with javascript to replace the second one, but i'm not able to write the function.

    Also, if you know of any form that works like this, a link to it would be greatly appreciated.
     
    Abh, May 23, 2010 IP
  2. wmhelp

    wmhelp Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Just write the form's HTML, surround them with DIVs
    <div id=form1><form..1..</form></div>
    <div id=form2 style="display:none"><form..2..</form></div>

    I would suggest using JQuery for whats next, but you can do it with naive JS as well:
    Add the Select box with onchange event <select onchange=changeFunc(this)></select>
    When the options will change on the select it will call the changeFunc() function:
    function changeFunc(obj) { // obj is the select element
    // here depending on the value of obj change the style of the divs
    }

    Hope it helps
     
    wmhelp, May 24, 2010 IP
  3. Abh

    Abh Active Member

    Messages:
    162
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Thanks for the tip, now i just need to get back to my javascript manual and learn to write changeFunc(obj) :)
     
    Abh, May 24, 2010 IP
  4. Lord

    Lord Active Member

    Messages:
    134
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #4
    i'd do it like this
    suppose this is your select
    <select onChange="showForm(this.value);"><option>Choose Form</option><option value="1">Form 1</option><option value="2">Form 2</option></select>
    Code (markup):
    and the forms wmhelp above
    
    <div id=form1><form..1..</form></div>
    <div id=form2 style="display:none"><form..2..</form></div>
    
    Code (markup):
    
    <script>
    function showForm(val)
    {
    if(val=='1') {document.getElementById('form1').style.display="";document.getElementById('form2').style.display="none";}
    if(val=='2') {document.getElementById('form2').style.display="";document.getElementById('form1').style.display="none";}
    }
    </script>
    
    Code (markup):
    enjoy
     
    Lord, May 27, 2010 IP
  5. Abh

    Abh Active Member

    Messages:
    162
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    60
    #5
    Thank you, i managed to do it like this:
    function test(divID){
        var element = document.getElementById('change').getElementsByTagName('div');
    
        for(var i = 0; i < element.length; i++ ) {
            if(element[i].id == divID){
                element[i].style.display = 'block';
            }
            else {
                element[i].style.display = 'none';
            }
        }
    } 
    Code (markup):
    but the second form does not get the values of the variables, i think i'll be able to solve that on the php side.
     
    Abh, May 28, 2010 IP