aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Setting the Value of a RadioButton on Load Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Setting the Value of a RadioButton on Load" Watch "Setting the Value of a RadioButton on Load" New topic
Author

Setting the Value of a RadioButton on Load

Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1490
    
    1
I am trying to use JavaScript to set the value of a RadioButton group but I can't get it to work.

Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
And what is happening? Error Message? You setting it onload?

Eric
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1490
    
    1
The radio group is not getting a value at all. The radio group does not have any selected item(first item should have a dot in it). It should have the first item selected since the value of the variable I am using is 1.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61768
    
  67

Set the checked property of the first radio element to true.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1490
    
    1
Can you help with the syntax? I can't seem to be able to povide the correct reference to set the checked property.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61768
    
  67

document.formName.radioName[0].checked = true;
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1490
    
    1
When I use this syntax I get an error that this is either not an object or is null.
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1490
    
    1
I still have not got this to work yet. Can you tell me why I have errors on the syntax yo suggested? I can never get the following syntax to work in my JavaScripts for JSF for some reason. It will work in JSP though.

document.form1.text3.value = "Steve";
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61768
    
  67

Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1490
    
    1
I have tried several differnt things to get this to work. The first two lines of this code work but the third one will not. Note the radio1 button group is located in a tabbedPanel.

Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1490
    
    1
This might not make any sense but if I put an alert() at the end of my JavaScript, I run the page, then I refresh the page, the radio buton is checked as expected. But after I click the ok on the alert the radio button becomes unchecked.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61768
    
  67

Sounds like a race condition to me. Are you sure that you are waiting until the document is loaded to try and manipulate the DOM?

Also "tabbedPanel" is not an HTML construct and is meaningless in this forum.
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1490
    
    1
I am firing my function in the onload event. If it is a race problem it does not seem that one radio button would have the checked property set and disreguard the second radio button.

If I could I would like to explain what I need to accomplish. My web app loads data from a connection to a data record using a servlet. this data record is based on user input selection. I assign the field values to variables. The user can open another form to modify the record data. Initialally the form controls are empty, the user fills out what ever and saves the form. This writes data into the data record. Later if the user calls the form I need it populated with data thats already been put in.

I don't know if this is this correct way or not. I have everthing working except the form populating with the pre-saved data.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61768
    
  67

Why are you doing it only the client side rather than setting it in the markup generated on the server?
 
jQuery in Action, 2nd edition
 
subject: Setting the Value of a RadioButton on Load