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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

change selected radiobutton 1

Status
Not open for further replies.

tomvdduin

Programmer
Sep 29, 2002
155
NL
I have the following part of a form:
Code:
<input type="radio" name="ABO_FAKTURERING" value="0" checked>aparte factuur
<input type="radio" name="ABO_FAKTURERING" value="1">verzamelfactuur 1
<input type="radio" name="ABO_FAKTURERING" value="2">verzamelfactuur 2
<input type="radio" name="ABO_FAKTURERING" value="3">verzamelfactuur 3

I want to change the checked value of these radiobuttons by a javascript. I tried it this way:
Code:
this.form_abonnement.ABO_FAKTURERING[1].value = 1
to check the radiobutton before 'verzamelfactuur 1', but IE gives me an error: 'this.form_abonnement.ABO_FAKTURERING.1 is null or not an object'. The form itself is named: form_abonnement.

What am I doing wrong? why is the dot between ABO_FAKTURERING and 1 there?

greetz,

Tom
 
Instead of:

Code:
this.form_abonnement.ABO_FAKTURERING[1].value = 1

...try:

Code:
[b]document[/b].form_abonnement.ABO_FAKTURERING[1].[b]checked = true[/b];

As for your error message, the ".1" instead of "[1]" is just the ways error messages appear. 'not exactly sure why javascript resorts to this since, as far as I can tell, you cannot code it this way (with the dot).

'hope this helps.

--Dave
 
Dave,

Thanks for your help, but it doesn't work... still the same error message, pointing at:
document.form_abonnement.ABO_FAKTURERING[1].checked = true;
and stating that document.form_abonnement.ABO_FAKTURERING.1 is null or not an object.

Any other suggestions?
 
Well, I created a tiny example to help me try this out and it works for me in IE6.

Can you show some more of your code (the form; whatever it is that triggers the action to set checked=true; and the javascript where this happens)?

Thanks.

--Dave
 
This worked for me:

Code:
<html>
<head>

<script language="JavaScript" type="text/javascript">
function catchevnt()
{
	document.form1.ABO_FAKTURERING[1].checked = true;
}
</script>
</head>
<body >
<form name="form1">
<input type=button name="b1" value="button1" onclick="catchevnt();">
<input type="radio" name="ABO_FAKTURERING" value="0" checked ID="Radio1">aparte factuur
<input type="radio" name="ABO_FAKTURERING" value="1" ID="Radio2">verzamelfactuur 1
<input type="radio" name="ABO_FAKTURERING" value="2" ID="Radio3">verzamelfactuur 2
<input type="radio" name="ABO_FAKTURERING" value="3" ID="Radio4">verzamelfactuur 3


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

Maybe the form name has a spelling mistake.

rsshetty.

It's always in the details.
 
Dave,

It's a page that's parsed by PHP. depending on a value in the database, a option must be checked when the page is opened.

Here some of the code (there's some PHP in it, hope it doesn't matter for you)

Code:
<form name="form_abonnement" action="<?php echo $_SERVER['PHP_SELF']."?sid=".$_GET['sid']."&action=".$_GET['action']?>" method="POST">

...

<SCRIPT language="JavaScript" type="text/javascript">
  this.document.form_abonnement.ABO_FAKTURERING[<?php echo $values['ABO_FAKTURERING']; ?>].value=1;
</SCRIPT>
	
Facturering:
<table border="0" cellpadding="0" cellspacing="0">
<tr>
  <td width="20">&nbsp;</td>
  <td valign="baseline">
    <input type="radio" name="ABO_FAKTURERING" value="0" checked>
  </td>
  <td>
    aparte factuur
  </td>
</tr>
<tr>
  <td>&nbsp;</td>
  <td valign="baseline">
    <input type="radio" name="ABO_FAKTURERING" value="1">
  </td>
  <td>
    verzamelfactuur 1
  </td>
</tr>
<tr>
  <td>&nbsp;</td>
  <td valign="baseline">
    <input type="radio" name="ABO_FAKTURERING" value="2">
</td>
  <td>
    verzamelfactuur 2
  </td>
</tr>
<tr>
  <td>&nbsp;</td>
  <td valign="baseline">
    <input type="radio" name="ABO_FAKTURERING" value="3">
  </td>
  <td>
    verzamelfactuur 3
  </td>			
</tr>
</table>

In the source of te page in my browser, I can see that the line:
this.document.form_abonnement.ABO_FAKTURERING[<?php echo $values['ABO_FAKTURERING']; ?>].value=1;
is correctly parsed by php. In the source, there stands:
this.document.form_abonnement.ABO_FAKTURERING[1].value=1;

Thanks in advance for looking at it!

Tom
 
In the above code-snippet, you still see this.document.form_abonnement.....
I removed 'this' also.. it doesn't matter...
 
Ah... I don't know PHP, but I think I see the problem. You are trying to set the value BEFORE the page draws. At that moment, the radio button DOESN'T exist in the mind of the HTML.

Rather, when you draw the page, make the BODY tag's onload event to setting 'document.form_abonnement.ABO_FAKTURERING[1].checked = true;'

Try that and let us know.

--Dave
 
Dave,

The sollution is all that simple, I didn't thought of it...

Thanks for your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top