Jump to content

[SOLVED] simple document.write problem!


unistake

Recommended Posts

Hi all, got a simple problem for you

 

I am trying to create an outcome by clicking on radio buttons, I cant seem to get the 'document.write' to work and display the text at the bottom of the radio buttons, with those radio buttons still showing. The code I have is:

<html> 
<head> 
<script type="text/javascript"> 

function calc (){

  selection = document.getElementsByName("typeofbet");

  for(i=0; i<selection.length; i++){ 
    if(selection[i].checked == true){
      var selected = selection[i].value;
    }
  }

  if(selected == "qualifier"){
   qualifier();
}else{
    if(selected == "freesr"){
      sr();
    }else{
      if(selected == "freesnr"){
        snr();
      }
    }
  }
}

function qualifier (){
document.write('this is the qualifier!');
}
   
function sr(){
document.write('this is the sr!');
}
   
function snr(){
document.write('this is the snr!');
}

</script> 
</head> 
<body> 
<form name="calcform"> 

<p> 
<input type="radio" name="typeofbet" value="qualifier" onClick="return calc();"> qual
<input type="radio" name="typeofbet" value="freesr" onClick="return calc();"> sn
<input type="radio" name="typeofbet" value="freesnr" onClick="return calc();"> snr
</p> 

</form> 
</body> 
</html>

Thanks for the heads up

Link to comment
https://forums.phpfreaks.com/topic/169141-solved-simple-documentwrite-problem/
Share on other sites

dont use document.write. it sucks. Instead make make a span or div tag, and use javascript to effect the innerHTML of the elemt. Something like

 

<html>
<head>
<script type="text/javascript">

function calc (){
text = document.getElementById("text");

  selection = document.getElementsByName("typeofbet");

  for(i=0; i<selection.length; i++){
    if(selection[i].checked == true){
      var selected = selection[i].value;
    }
  }

  if(selected == "qualifier"){
      qualifier();
   }else{
    if(selected == "freesr"){
      sr();
    }else{
      if(selected == "freesnr"){
        snr();
      }
    }
  }
}

function qualifier (){
   //document.write('this is the qualifier!');
text.innerHTML = 'this is the qualifier!'
}
   
function sr(){
  // document.write('this is the sr!');
text.innerHTML = 'this is the sr!'
}
   
function snr(){
  // document.write('this is the snr!');
text.innerHTML = 'this is the snr!'
}

</script>
</head>
<body>
<form name="calcform">

<p>
<input type="radio" name="typeofbet" value="qualifier" onClick="return calc();"> qual
<input type="radio" name="typeofbet" value="freesr" onClick="return calc();"> sn
<input type="radio" name="typeofbet" value="freesnr" onClick="return calc();"> snr
</p>
<span id="text"></span>

</form>
</body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.