#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2001
    Posts
    3
    Rep Power
    0

    Question Javascript and Form action attribute


    Hi folks I wonder if anyone can help I'm trying to switch the action attribute of form based on the value of a select box when the submit button is pressed theproblem are that:
    1: the browser seems to fall through the if else structure to the end
    2: the browser does not go to the relevant page

    here's the code

    function sniffer() {
    if (document.form1.Version=="Flash") {
    document.form1.action=="Flash.htm";
    return alert ("Flash Page");
    }
    else if (document.form1.Version=="NonFlash") {
    document.form1.action=="NonFlash.htm";
    return alert ("Non Flash Page");
    }


    }
    Version is the name of the select element in the form I'm getting no bugs coming through the browser so I'm stuck for new ideas any help would be gratefully received

    thanks
  2. #2
  3. No Profile Picture
    Seņor Member
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2000
    Posts
    1,157
    Rep Power
    38
    The select list is the only form element that doesn't have a value property. It contains other objects, known as options, that have both a value and text property. The value property is what is passed back to your script when the form is submitted and the text value is what the user sees in the drop down.

    Therefore, you need to access via document.form.select.options[document.form.select.options["value"].selected].value. I'm pretty certain I got that syntax right - if you still have problems, let me know and I will double-check it.
    Michael
  4. #3
  5. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16
    You've got some other problems: using the equality operator (==) for assignment (=); and returning tha value of an alert (?). Also, don't confuse syntax 'bugs' with runtime errors - the latter may run smoothly while not the effecting the desired result.


    Code:
    function Flash_or_not(frm) { 
            if (frm.Version.options[frm.Version.selectedIndex].value == 'Flash') { 
                frm.action = 'Flash.htm'; 
                alert ("Flash Page"); 
       }  else if (frm.Version.options[frm.Version.selectedIndex].value == 'NonFlash') { 
                frm.action = 'NonFlash.htm'; 
                alert ('Non Flash Page');
       } 
    }
    
    
    <form name="form1">
    <select name="Version">
    <option value="Flash">Flash</option>
    <option value="NonFlash">Not a Flash</option>
    </select>
    <input type="submit" value="Submit" onclick="Flash_or_not(this.form)">
    </form>
    Passing the .form property (which refers to the containing form object - every element has one) simplifies references in the function. More...
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2001
    Posts
    3
    Rep Power
    0

    Unhappy more probs


    the browser is now displaying error 405 something to do with the mime type how do I set it? I think it is something to do with the method attribute in the form. Can I use the post method?
    Last edited by Boab; May 7th, 2001 at 09:04 AM.
  8. #5
  9. No Profile Picture
    Seņor Member
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2000
    Posts
    1,157
    Rep Power
    38
    Could you cut and paste the error in this forum?
    Michael
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2001
    Posts
    3
    Rep Power
    0

    Got it sorted


    Thanx folks got the problem fixed just messed about with it for a while but again thanx for the help

IMN logo majestic logo threadwatch logo seochat tools logo