/    Sign up×
Community /Pin to ProfileBookmark

Redirect to a different page based on the selected radio and submit button

Hallo!
Can anyone help me?
I am a beginner at this. I have a lot of problem with this code. I don’t know where it went wrong.
A process occurs when I press the “open page” button without selecting the radio button and click “OK” pop-up,
and click the “cancel” button on the “OK” (confirmation) pop-up, and I don’t want that to happen.
Thank you in advance.

Sample code :

[code]<!DOCTYPE html>
<html>

<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<script type=”text/javascript”>
function numberGroup() {
var group = document.myNumber.number;
for (var i = 0; i < group.length; i++) {
if (group[i].checked)
break;
}
if (i == group.length)
return alert(“Please select a number”);
return confirm(“Number ” + (i + 1) + ” selected. Click OK to Confirm”)
}

function usePage(frm, nm) {
for (var i = 0, numbers = frm.elements; i < numbers.length; i++)
if (numbers[i].name == nm && numbers[i].checked)
frm.action = numbers[i].value;
}
</script>
</head>

<body>
<div>
<form name=”myNumber” onsubmit=”usePage(this, ‘number’);” target=”_blank”>
<input type=”radio” name=”number” value=”Link1″>
<input type=”radio” name=”number” value=”Link2″><br /><br />
<input type=”submit” value=”Go To Page” onclick=”numberGroup()” />
</form>
</div>
</body>

</html>[/code]

to post a comment
HTMLJavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumDec 26.2020 — You can easily get the radio button being checked by use of the pseudo class `:checked</C>, no need for looping or searching.

In your function <C>
numberGroup</C> you are not able to control whether the form is submitted or not. You need to do this in the onsubmit handler.

This code works for me:
<CODE>&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;

&amp;lt;head&amp;gt;
&amp;lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&amp;gt;
&amp;lt;script type=&quot;text/javascript&quot;&amp;gt;
function usePage(frm, ev) {
// get radio button being checked
const checkedBtn = document.querySelector('input[type=&quot;radio&quot;][name=&quot;number&quot;]:checked');
// was a button checked?
if (checkedBtn) {
// let user confirm
const result = confirm(&quot;Link &quot; + checkedBtn.value + &quot; selected. Click OK to Confirm&quot;);
if (result) {
// user confirmed
frm.action = checkedBtn.value;
} else {
// user denied
// prevent form from being submitted
ev.preventDefault();
}
} else {
// no radio button checked
// notify user
alert(&quot;Please select a number&quot;);
// prevent form from being submitted
ev.preventDefault();
}
}
&amp;lt;/script&amp;gt;
&amp;lt;/head&amp;gt;

&amp;lt;body&amp;gt;
&amp;lt;div&amp;gt;
&amp;lt;form name=&quot;myNumber&quot; onsubmit=&quot;usePage(this, event);&quot; target=&quot;_blank&quot;&amp;gt;
&amp;lt;input type=&quot;radio&quot; name=&quot;number&quot; value=&quot;Link1&quot;&amp;gt;
&amp;lt;input type=&quot;radio&quot; name=&quot;number&quot; value=&quot;Link2&quot;&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;br /&amp;gt;
&amp;lt;input type=&quot;submit&quot; value=&quot;Go To Page&quot; /&amp;gt;
&amp;lt;/form&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/body&amp;gt;

&amp;lt;/html&amp;gt;
</CODE>

BTW: You better use code tags: <C>
your code here`
when posting code. I edited your posting accordingly.
Copy linkTweet thisAlerts:
@dikoauthorDec 27.2020 — @Sempervivum#1626190

Wow, you are great, thank you so much for helping me. i really... it 😄
Copy linkTweet thisAlerts:
@TasiAug 23.2022 — Can we not do this without opening a new tab and without any pop-up message? If yes, please reply. I am also a complete beginner in Javascript if anyone can help me with it, it would be very kind.



Copy linkTweet thisAlerts:
@TasiAug 23.2022 — @Sempervivum#1626190 hi can we not do this without opening a new tab and without any pop-up message. I am also a complete beginner in Javascript if you can help me with this, it would be very kind.
Copy linkTweet thisAlerts:
@SempervivumAug 23.2022 — @Tasi#1646438

Surely this can be done. Apart from opening in a new tab, is your task the same: Open a new page in the same tab based on the value of a radio button?
×

Success!

Help @diko spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...