I have devised this script to redirect to the specific page based on the radio selection but it keeps going to the old link that was setup. Are there any recommendations you can suggest to get this working?
<div class="col-12" style="margin-top">
<label><strong>What Is Your Business Currently Generating?</strong></label>
<div class="d-flex m-t--20">
<div class="p--relative check">
<input type="radio" id="o5kradio" name="income" value="o5k" checked required/>
<label for="diy">Over $5k Per Month</label>
</div>
<div class="p--relative check">
<input type="radio" id="u5kradio" name="income" value="u5k" required/>
<label for="hands-on">Under $5k Per Month</label>
</div>
</div>
</div>
<script>
var o5kradio = document.getElementById("o5kradio"),
u5kradio = document.getElementById("u5kradio"),
submitButton = document.getElementById("SubmitButton");
submitButton.addEventListener("click", function () {
if (o5kradio.checked) {
alert('o5k');
window.location = "https://vincedelmonte7figuremastermind.com/sit-in/schedule-now/";
} else {
alert('u5k');
window.location = "https://vincedelmonte7figuremastermind.com/sit-in/schedule-now-2/";
}
}, false);
</script>
<!--ERROR/SUCCESS MESSAGE-->
<div class="message js-message"></div>
<button type="submit" value="submit" id="SubmitButton" class="btn txt--shadow-smaller">Click TO SUBMIT APPLICATION</button>
</form>
Sends users here no matter the selection: https://vincedelmonte7figuremastermind.com/sit-in/schedule-now/
2
Answers
Okay, so this works and while it’s a bit of overkill, it’s very thorough overkill that you can use for any future radio button forms, even ones with dozens of options:
So to clarify, here’s what I’m doing…
For starters, I prefer “onclick” functions in buttons because it gives more freedom. I created a function called “submit” and it passes two values from the HTML: 1. “this”, which will be its own Id so the function can identify the source and 2. an array called “options” which contains the Ids of all of the radio buttons it will need to evaluate to find the checked one:
Next, the first part of the function finds the checked radio button by cycling through the array. Since there can only ever be one checked button, this is easy to do:
Next, I used a nested switch to decide how to handle the results. Note that adding “.id” after “source” and “options” bypasses the need to use “getElementById”:
Once again, this is overkill, and I wouldn’t be surprised if I’ve made at least one faux pas here, but the beauty of this setup is that every radio button form you have can utilize the same function, and you can have as many options as you want.
All you need to do is add another “case” to the “source.id” switch for each time a user will click the “submit” button (on additional pages, I assume) and you can control however many pages, redirections, and options you may have with one function. This means each click will only ever make three calculations (for statement, source switch, and option switch) so as to avoid multiple “else if” statements in a row for more than two radio buttons.
PS. The Id for “submitButton” starts with a capital “S”, though fixing that didn’t get it to work for me.
Here is one approach to achieve page redirects based on radio button selection: