Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Special radio button validation 2

Status
Not open for further replies.

dodge20

MIS
Joined
Jan 15, 2003
Messages
1,048
Location
US
I have a form that takes the value of a checked radio button and passes that to another button. Then that new button passes its checked value to another new button.

My problem is, once the third button is checked, you can go back and change the first button, but the third button value still remains the same. For example:

radgroup1 value x checked
radgroup2 value x checked
radgroup3 value x checked

Then you decide to change radgroup1 to y so you have
radgroup1 value y checked
radgroup2 value x checked
radgroup3 value x checked

When this can't be because you are passing the values from radiogroup to radiogroup. So, ideally, when you change x to y, it would then in turn change all the x's to y's. Here is a section of my code. If it helps, this is a basketball bracket for those of you who know what that is.

Code:
<script>
function dothis(obj1,obj2,obj3)
{
var frm = document.frm
for(i =0; i <frm[obj1].length; i++)
{
if(frm[obj1][i].checked)
var temp = frm[obj1][i].value
}
for(i =0; i <frm[obj2].length; i++)
{
if(frm[obj2][i].checked)
var temp2 = frm[obj2][i].value
}
document.frm[obj3][0].value = temp;
document.frm[obj3][1].value = temp2;
document.getElementById(obj3+'_0').innerHTML = temp;
document.getElementById(obj3+'_1').innerHTML = temp2;
}
function dothis2(obj)
{
var frm = document.frm
for(i =0; i <frm[obj].length; i++)
{
if(frm[obj][i].checked)
var temp = frm[obj][i].value
}
document.getElementById('me').innerHTML = "THE WINNER IS \n"+temp
}
</script>

<input type="radio" name="r1g8" value="Gonzaga"
		  onclick = "dothis('r1g7','r1g8','r2g4');">
          <FONT FACE="Arial Narrow, Arial" SIZE="2"><font face="Times New Roman, Times, serif">Gonzaga</font></FONT></label>
          <br>
          <label>
          <input type="radio" name="r1g8" value="Valparasio"
		  onclick = "dothis('r1g7','r1g8','r2g4');">
        <FONT FACE="Times New Roman, Times, serif" SIZE="2">Valparasio

<input type="radio" name="r2g4"onclick = "dothis('r2g3','r2g4','r3g2');">
          <span id = "r2g4_0"></span>
  </label>
          <br>
          <label>
          <input type="radio" name="r2g4" onclick = "dothis('r2g3','r2g4','r3g2');">
          <span id = "r2g4_1"></span>
  </label>
      <input type="radio" name="r3g2" onclick = "dothis('r3g3','r3g4','r4g1');">		  
          <span id = "r3g2_0"></span>
  </label>
          <br>
          <label>
          <input type="radio" name="r3g2" onclick = "dothis('r3g3','r3g4','r4g1');">		  
          <span id = "r3g2_1"></span>
  </label>



Dodge20
 
Apologies for the late reply, figured it would be easier to make it and then you can figure it out and alter it to what you want. Turned out to be a little more complex than I thought it would be. This is what I came up with:
Code:
<html>
  <head>
    <title>Bracket</title>
    <link rel="stylesheet" href="mystyle.css" type="text/css" media="screen"/>
    <script type="text/javascript">
var teams = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15",
"16","17","18","19","20","21","22","23","24","25","26","27","28",
"29","30","31","32","33","34","35","36","37","38","39","40","41",
"42","43","44","45","46","47","48","49","50","51","52","53","54",
"55","56","57","58","59","60","61","62","63","64"];
function buildBracket(){
  var node, noteams;
  var boxno = 1;
  var width = 60;
  var height = 20;
  var xgap = 20;
  document.styleSheets[0].addRule("input", "position: absolute; width: " + width + "px; height: " + height + "px;", 0);
  for (var noteams=1;noteams<=teams.length;noteams=noteams*2){
    for (var i=0;i<noteams;i++){
      box = document.createElement("div");
      input = document.createElement("input");
      input.type = "button";
      if (noteams==teams.length) input.value = teams[i];
      input.style.left = (width+xgap)*Math.log(teams.length/noteams)/Math.log(2)+"px";
      input.style.top = ((i*teams.length*height/noteams)+(teams.length*height*0.5/noteams)-
(height/2))+"px";
      input.onclick = function(){changeValue(this);};
      box.appendChild(input);
      input = document.createElement("input");
      input.name = "box"+boxno;
      input.type = "hidden";
      if (noteams==teams.length) input.value = teams[i];
      box.appendChild(input);
      node = document.forms[0];
      if (boxno==1) node.insertBefore(box, node.childNodes[0]);
      else{
        node = node.firstChild;
        bbn = (boxno).toString(2);
        for (var j=1;j<bbn.length-1;j++){
          node = node.childNodes[parseInt(bbn.charAt(j))];
        }
        node.insertBefore(box, node.childNodes[node.childNodes.length-2]);
      }
      boxno++;
      if (noteams<teams.length){
        div = document.createElement("div");
        div = document.forms[0].appendChild(div);
        div.style.position = "absolute";
        div.style.left = ((width+xgap)*Math.log(teams.length/noteams)/Math.log(2))-xgap+"px";
        div.style.top = (i*teams.length*height/noteams)+(teams.length*height/(noteams*2))
-(teams.length*height*0.5/(noteams*2))+"px";
        div.style.width = xgap/2 + "px";
        div.style.height = (teams.length*height*0.5/noteams)+"px";
        div.style.border = "1px solid black";
        div.style.borderLeft = "none";
        div = document.createElement("div");
        div = document.forms[0].appendChild(div);
        div.style.position = "absolute";
        div.style.left = ((width+xgap)*Math.log(teams.length/noteams)/Math.log(2))-(xgap/2)+"px";
        div.style.top = (i*teams.length*height/noteams)+(teams.length*height*0.5/noteams)+"px";
        div.style.width = xgap/2 + "px";
        div.style.height = "1px";
        div.style.borderTop = "1px solid black";
      }
    }
  }
}
function changeValue(node){
  if (node.value!='){
    if (node.parentNode.parentNode.nodeName != "FORM"){
      node.parentNode.parentNode.childNodes[2].value = node.value;
      node.parentNode.parentNode.childNodes[3].value = node.value;
      if (node.parentNode==node.parentNode.parentNode.childNodes[0]) changeValue1(node.parentNode.parentNode.childNodes[2], node.parentNode.parentNode.childNodes[1].lastChild.value);
      else changeValue1(node.parentNode.parentNode.childNodes[2], node.parentNode.parentNode.childNodes[0].lastChild.value);
    }
  }
}
function changeValue1(node, valueToChange){
  if (node.parentNode.parentNode.nodeName != "FORM" && node.parentNode.parentNode.childNodes[2].value!=' && node.parentNode.parentNode.childNodes[2].value==valueToChange){
    node.parentNode.parentNode.childNodes[2].value = node.value;
    node.parentNode.parentNode.childNodes[3].value = node.value;
    if (node.parentNode==node.parentNode.parentNode.childNodes[0]) changeValue1(node.parentNode.parentNode.childNodes[2], valueToChange);
    else changeValue1(node.parentNode.parentNode.childNodes[2], valueToChange);
  }
}
    </script>
  </head>
  <body onload="buildBracket()">
    <form action="storebracket.asp" method="post"><input style="position: relative; float: right; width: 100px;" type="submit" value="Submit bracket"/></form>
  </body>
</html>
 
Here's an update to JontyMC's code. It will allow the script to work in Gecko based browsers (Firefox, Mozilla, Netscape, etc...), and it might also allow it to work in KHTML based browsers as well, although I don't know for sure.

Code:
<html>
  <head>
    <title>Bracket</title>
    <link rel="stylesheet" href="mystyle.css" type="text/css" media="screen"/>
    <script type="text/javascript">
var teams = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59","60","61","62","63","64"];
function buildBracket(){
  var node, noteams;
  var boxno = 1;
  var width = 60;
  var height = 20;
  var xgap = 20;
  if(navigator.userAgent.indexOf("MSIE")>=0) {
    document.styleSheets[0].addRule("input", "position: absolute; width: " + width + "px; height: " + height + "px;", 0);
  } else { //--Credits: [URL unfurl="true"]http://www.mozilla.org/docs/dom/domref/dom_style_ref14.html#1003775--//[/URL]
    document.styleSheets[0].insertRule("input{position: absolute; width: " + width + "px; height: " + height + "px;}", 0);
  }
  for (var noteams=1;noteams<=teams.length;noteams=noteams*2){
    for (var i=0;i<noteams;i++){
      box = document.createElement("div");
      input = document.createElement("input");
      input.type = "button";
      if (noteams==teams.length) input.value = teams[i];
      input.style.left = (width+xgap)*Math.log(teams.length/noteams)/Math.log(2)+"px";
      input.style.top = ((i*teams.length*height/noteams)+(teams.length*height*0.5/noteams)-(height/2))+"px";
      input.onclick = function(){changeValue(this);};
      box.appendChild(input);
      input = document.createElement("input");
      input.name = "box"+boxno;
      input.type = "hidden";
      if (noteams==teams.length) input.value = teams[i];
      box.appendChild(input);
      node = document.forms[0];
      if (boxno==1) node.insertBefore(box, node.childNodes[0]);
      else{
        node = node.firstChild;
        bbn = (boxno).toString(2);
        for (var j=1;j<bbn.length-1;j++){
          node = node.childNodes[parseInt(bbn.charAt(j))];
        }
        node.insertBefore(box, node.childNodes[node.childNodes.length-2]);
      }
      boxno++;
      if (noteams<teams.length){
        div = document.createElement("div");
        div = document.forms[0].appendChild(div);
        div.style.position = "absolute";
        div.style.left = ((width+xgap)*Math.log(teams.length/noteams)/Math.log(2))-xgap+"px";
        div.style.top = (i*teams.length*height/noteams)+(teams.length*height/(noteams*2))-(teams.length*height*0.5/(noteams*2))+"px";
        div.style.width = xgap/2 + "px";
        div.style.height = (teams.length*height*0.5/noteams)+"px";
        div.style.border = "1px solid black";
        div.style.borderLeft = "none";
        div = document.createElement("div");
        div = document.forms[0].appendChild(div);
        div.style.position = "absolute";
        div.style.left = ((width+xgap)*Math.log(teams.length/noteams)/Math.log(2))-(xgap/2)+"px";
        div.style.top = (i*teams.length*height/noteams)+(teams.length*height*0.5/noteams)+"px";
        div.style.width = xgap/2 + "px";
        div.style.height = "1px";
        div.style.borderTop = "1px solid black";
      }
    }
  }
}
function changeValue(node){
  if (node.value!=''){
    if (node.parentNode.parentNode.nodeName != "FORM"){
      node.parentNode.parentNode.childNodes[2].value = node.value;
      node.parentNode.parentNode.childNodes[3].value = node.value;
      if (node.parentNode==node.parentNode.parentNode.childNodes[0]) changeValue1(node.parentNode.parentNode.childNodes[2], node.parentNode.parentNode.childNodes[1].lastChild.value);
      else changeValue1(node.parentNode.parentNode.childNodes[2], node.parentNode.parentNode.childNodes[0].lastChild.value);
    }
  }
}
function changeValue1(node, valueToChange){
  if (node.parentNode.parentNode.nodeName != "FORM" && node.parentNode.parentNode.childNodes[2].value!='' && node.parentNode.parentNode.childNodes[2].value==valueToChange){
    node.parentNode.parentNode.childNodes[2].value = node.value;
    node.parentNode.parentNode.childNodes[3].value = node.value;
    if (node.parentNode==node.parentNode.parentNode.childNodes[0]) changeValue1(node.parentNode.parentNode.childNodes[2], valueToChange);
    else changeValue1(node.parentNode.parentNode.childNodes[2], valueToChange);
  }
}
    </script>
  </head>
  <body onload="buildBracket()">
    <form action="storebracket.asp" method="post"><input style="position: relative; float: right; width: 120px;" type="submit" value="Submit bracket"/></form>
  </body>
</html>

Basically there are two changes.
Here:
Code:
  if(navigator.userAgent.indexOf("MSIE")>=0) {
    document.styleSheets[0].addRule("input", "position: absolute; width: " + width + "px; height: " + height + "px;", 0);
  } else { //--Credits: [URL unfurl="true"]http://www.mozilla.org/docs/dom/domref/dom_style_ref14.html#1003775--//[/URL]
    document.styleSheets[0].insertRule("input{position: absolute; width: " + width + "px; height: " + height + "px;}", 0);
  }
and here:
Code:
    <form action="storebracket.asp" method="post"><input style="position: relative; float: right; width: 120px;" type="submit" value="Submit bracket"/></form>
The second one is just a visual change, so that all the text appears within the button. However, it is recommended that you do not use fixed widths for buttons (such as px). People who may resize their fonts larger or smaller may have problems viewing the page. In addition, browsers that use different fonts will also look different, and may cut off words/numbers if the font is really big. I don't know how to get around this in your case, but if you can find a way without using fixed sizes, that would solve it.
 
That is great guys. Stars for you both. I will work with this. Is there a quick way to change the font and font size? I tried, but I am lost on this entire thing. I have never used CSS.

Thanks again.

Dodge20
 
Could create your own .css file called "mystyle.css" which just has the following:
Code:
body
{
  font-family: times;
  font-size: large;
  font-weight: bold;
}
Or put it directly in the page (under link element):
Code:
<style type="text/css>
body
{
  font-family: times;
  font-size: large;
  font-weight: bold;
}
</style>
Instead of specifying body, you could specify input, or form, or whatever.

I agree with Kevin, I should've made the code more accessible, perhaps positioning the divs rather than the input.
 
Thanks.

Is there an easy way to do what Kevin said?

Dodge20
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top