#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Barbados
    Posts
    149
    Rep Power
    62

    Conditional form field


    Hi,

    I want to create a form that has a conditional question, but cannot find anything anywhere to help.

    Basically,

    Do you () drive to work () use public transportation

    If the answer is "use public transportation", I want to display another radio button question

    Do you primarily use the
    () train () bus () other

    I understand that it can be done using Javascript and CSS,.
    Can anyone assist?

    Thanks

    SiRiC
  2. #2
  3. No Profile Picture
    http://stealthwd.ca
    Devshed Novice (500 - 999 posts)

    Join Date
    Dec 2005
    Posts
    706
    Rep Power
    203
    Originally Posted by siric
    Hi,

    I want to create a form that has a conditional question, but cannot find anything anywhere to help.

    Basically,

    Do you () drive to work () use public transportation

    If the answer is "use public transportation", I want to display another radio button question

    Do you primarily use the
    () train () bus () other

    I understand that it can be done using Javascript and CSS,.
    Can anyone assist?

    Thanks

    SiRiC
    The easiest way would be to have the ()train ()bus ()other in a hidden div, and when "use public transportation" is checked to unhide it

    so hidden div is something like

    Code:
    <div id="transContain" style="display:none;">
    <input type="radio" id="train" value="train"/>
    </div>
    and then in your "use public transporation" button you have an onclick event to a function that would do something like

    Code:
    document.getElementById("transContain").style.display = "block";

    Comments on this post

    • sir_drinxalot agrees
  4. #3
  5. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Aug 2005
    Location
    Bucharest ROMANIA
    Posts
    2,524
    Rep Power
    608
    Originally Posted by Dameon51
    The easiest way would be to have the ()train ()bus ()other in a hidden div, and when "use public transportation" is checked to unhide it
    Eeer... not really. A "hidden" form element, on using CSS visibility or display, remains in the the DOM tree, thus the form will still send its value, if any, on submit. In other words if you make an element "hidden" that will not make it "disabled" as well.

    Either you disable/enable that element whenever you make it disappear/appear, or you use DOM methods to create/append/remove that element (createElement(), appendChild(), removeChild()... and so on)
  6. #4
  7. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2004
    Location
    Switzerland
    Posts
    1,152
    Rep Power
    1905
    Well sending those additional fields shouldn't be much of a problem because you can just ignore them.

    But if you must remove them then you should create the form with everything it could possibly require and then use javascript to remove the parts you don't need when the page is loaded. That way the form remains functional even without javascript...
    - Hugh of Borg

    The first thing young borg are taught: Keep away from Microsoft software!
  8. #5
  9. No Profile Picture
    http://stealthwd.ca
    Devshed Novice (500 - 999 posts)

    Join Date
    Dec 2005
    Posts
    706
    Rep Power
    203
    Originally Posted by KorRedDevil
    Eeer... not really. A "hidden" form element, on using CSS visibility or display, remains in the the DOM tree, thus the form will still send its value, if any, on submit. In other words if you make an element "hidden" that will not make it "disabled" as well.

    Either you disable/enable that element whenever you make it disappear/appear, or you use DOM methods to create/append/remove that element (createElement(), appendChild(), removeChild()... and so on)
    If the user doesn't check anything though the value submitted will be blank. Yeah you'll probably want to put some more conditions in there, like unchecking the checked value if that div hides again, but a blank submit is should good enough, depending on whats going on server side.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Barbados
    Posts
    149
    Rep Power
    62

    [solved]


    Thanks everyone. Got it going and yes, I will just ignore the variables that I don't need.

IMN logo majestic logo threadwatch logo seochat tools logo