Forum Moderators: open

Message Too Old, No Replies

Using parsed URL data to change default checked RADIO button

         

aberfeld

6:40 pm on Dec 18, 2008 (gmt 0)

10+ Year Member



Ok i realize this issue was addressed similarly using a select box rather than a radio button, and it works wonderfully...

here's the link to the forum where i got the code... i know it's old..
[webmasterworld.com...]

specifically the post by rocknbil about 4 posts down.
that example works beautifully, but I'm wondering if there's a way to port it to use radio buttons instead of a dropdown...

i found this:
[codingforums.com...]
specifically the solution presented by vwphillips 3 posts down.

but it seems to use an onload rather than a parsed url string...

is there a way to combine the two?

it seems like i'm just missing something and haven't been able to connect the dots.

thanks!

Greven

8:41 pm on Dec 18, 2008 (gmt 0)

10+ Year Member



The function from the second post(assuming it works) should be all you need. Lets say you have:

var myGetValue = parseURL("key"); //Holds the value from your URL
function checkV(f,v){
var c = f.elements['color'];
for(var i=0;i<c.length;i++){
c[i].checked=(c[i].value==v)?true:false;
}
}
checkV(myForm,myGetValue);

Keep in mind that you can't execute this code until your DOM is ready, as f is going to be null until that time(the same thing applies for the code from the first post, as the <select> would not exist yet).

aberfeld

9:22 pm on Dec 18, 2008 (gmt 0)

10+ Year Member



i'm not sure why but i'm having a slight bit of trouble figuring out how to apply that... or get it to work..

it seems like i should be able to apply your code to that second example directly replacing the script that's in the head section.. but for some reason i can't get it to parse correctly...

perhaps i'm using the wrong string?
would i use: ... .html?name=red
or .html?key=red

either of those aren't working...

ps i did test the script as is in the second post off of which you based your mod and it seems to work, yours has to also but i'm maybe confused as to how to apply it.

your help is greatly appreciated!

Greven

9:41 pm on Dec 18, 2008 (gmt 0)

10+ Year Member



Is the issue that you can't get "red"? Or that you're getting "red" but not it's not selecting?

If your URL is:
example.com/index.html?name=red
And you're using the parse function from the first post, you would want(modified by me):


[pre]
function myParse() {
var pqs = document.location+''; // Insures string
var pairs=pqs.split('?');
if ( pairs.length > 0){
var getArgs = pairs[1].split("&");
for (i=0;i<getArgs.length;i++) {
var keyval = getArgs[i].split('=');
if (keyval[0] == 'name') {
var nm = keyval[1];
break;
}
}
if (nm) {
return nm;
}
}
}
[/pre]

You would then feed the result into checkV.


checkV(myForm,myParse());

aberfeld

9:57 pm on Dec 18, 2008 (gmt 0)

10+ Year Member



I don't mean to be daft...
I'm working between the literal script and the script i adapted and just want to be sure i'm on the same page with you.

this is what i have right now on my screen:
<body>
<form name="user">
<input name="userLevel" type="radio" value="low">low
<input name="userLevel" type="radio" value="average">average
<input name="userLevel" type="radio" value="high">high
</form>
<script type="text/javascript">
var formSelect = myParse();
if (formSelect) {
for (i=0;i<document.user.userLevel.length;i++){
if (document.user.userLevel.options[i].value==formSelect){
document.user.userLevel.selectedIndex=i;
break; // No need to continue loop if found
}
}
}

function myParse() {
var pqs = document.location+''; // Insures string
var pairs=pqs.split('?');
if ( pairs.length > 0){
var getArgs = pairs[1].split("&");
for (i=0;i<getArgs.length;i++) {
var keyval = getArgs[i].split('=');
if (keyval[0] == 'name') {
var nm = keyval[1];
break;
}
}
if (nm) {
return nm;
}
}
}
checkV(user,myParse());

</script>
</body>

in your code i changed the checkV to be my form's name... thinking that would connect the two but it doesn't seem to work still... i get the three radio boxes but can't seem to get my string check any of them..

i'm making headway i feel like though...

Greven

10:39 pm on Dec 18, 2008 (gmt 0)

10+ Year Member



Here's the test I did. It uses the function I gave in the second post. Keep in mind it's really elementary and has no error checking, and you'd really be better off implementing a getElementsByClassName or the like to only get the ones you want back instead of all the inputs, but this works in IE7 when opened as http://example.com/test.html?name=average:

[pre]
<html>
<head>
<script>
function myParse() {
var pqs = document.location+''; // Insures string
var pairs=pqs.split('?');
if ( pairs.length > 0){
var getArgs = pairs[1].split("&");
for ( i = 0; i < getArgs.length; i++) {
var keyval = getArgs[i].split('=');
if (keyval[0] == 'name') {
var nm = keyval[1];
break;
}
}
if (nm) {
return nm;
}
}
}
function checkV(f,v){
var c = f.getElementsByTagName('input');
for(var i = 0 ; i < c.length; i++){
c[i].checked=(c[i].value==v)?true:false;
}
}
</script>
</head>
<body onload="checkV(document.getElementById('user'),myParse());">
<form name="user" id="user">
<input name="userLevel" type="radio" value="low">low
<input name="userLevel" type="radio" value="average">average
<input name="userLevel" type="radio" value="high">high
</form>
</body>
</html>
[/pre]

aberfeld

10:57 pm on Dec 18, 2008 (gmt 0)

10+ Year Member



brilliant.

the form is going to be used on a signup form but it's being linked to from a page before it where the user selects one of three options, each linking to the form but with a different selection... so very little user input ...

i think this will work great, your help was invaluable!

when you said "only get the ones you want back instead of all the inputs" is that to say that when the final form is submitted, it won't show that selection in the data sent?

Greven

4:19 am on Dec 19, 2008 (gmt 0)

10+ Year Member



No, I meant to improve speed and accuracy, you would only loop through the inputs that are necessary instead of all of them.