1

I have several checkboxes in a switch statement like the one below:

function whatsChecked(obj) {
    var indx = obj.id.substring(obj.id.length-1, obj.id.length);
    switch ( indx ) {
        case '9':
            if (document.sport.soccer_9.checked) {
                //window.open.href = "../google.com";;
                window.open("../google.com"); 
            } 
            break;
        }
    }
}

How can I use a continue button to determine which checkbox was clicked and redirect me to the correct page (instead of giving each checkbox it's own onclick event)?

2 Answers 2

1

Also at http://jsfiddle.net/amelvin/YkFza/

In this example all the change events of the checkboxes register themselves, and if you check one of the boxes the value of the checkbox is alerted out. If you change the alert(this.value) to location.href(this.value) you will have your redirect.

<form>
Google: <input type="checkbox" name="Google" value="http://google.com" />
Yahoo: <input type="checkbox" name="Yahoo" value="http://yahoo.com" />
</form>  

<script>
function AddHandlers(f)
{
    var change_handler = new Function("alert(this.value)");

    for (var i=0; i<f.elements.length; i++)
     {
        var e = f.elements[i];
         e.onchange = change_handler;    
     }
}

AddHandlers(document.forms[0]);
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

I would make a variable, that switches depending on which checkbox is checked. (Perhaps an int value)

And then, when you click 'continue' you can say If (click == 1) { window.open(www.google.com") } or something to that effect. Probably not the most elegant solution, but it certainly should work.

2 Comments

how would that work with each checkbox going to a different link? for example checkbox 1 goes to google and checkbox 2 goes to yahoo, etc.
If every checkbox has a different int value, then you can check it through an if or switch statement. But go with @amelvin's answer. His code is much more fluid and versatile because my code relies on two values essentially, and his only relies on one. (The value value)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.