/    Sign up×
Community /Pin to ProfileBookmark

dynamically changing radio buttons

Hi everyone. I was wondering if there was a way to dynamically change the value of radio buttons. I’ve tried using this code:

document.forms[“form1”][“radioBtn1”].value = “change description”;

When I use this script for checkboxes it works fine, so I’m wondering if it is even possible to munipulate radio button values using javascript. Thanks for your help.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisAug 04.2006 — if it is even possible to munipulate radio button values using javascript.[/QUOTE]
How are you determining that it did not change the value? Are you submitting the code, or using an alert, or something else?

Do you actually just want to see the black dot?
<i>
</i>document.forms["form1"]["radioBtn1"].click();

or
<i>
</i>document.forms["form1"]["radioBtn1"].checked = true;

will set the button.
Copy linkTweet thisAlerts:
@mark_yiehauthorAug 04.2006 — No I actually want to change the text for the radio button. I'm beginning to realize that you have to change the <label> tag don't you? Here is the HTML code that I used:

<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;script type="text/javascript" src="radio.js"&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form id="form1" name="form1" action="#"&gt;
&lt;input type="radio" name="radioSet" id="radioSet1" /&gt;
&lt;label for="radioSet1"&gt;Button 1&lt;/label&gt;&lt;br /&gt;
&lt;input type="radio" name="radioSet" id="radioSet2" /&gt;
&lt;label for="radioSet2"&gt;Button 2&lt;/label&gt;&lt;br /&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;


Then the javascript looks like this:

<i>
</i>window.onload = function() {

var radioBtn = document.forms["form1"]["radioSet"][0];
radioBtn.checked = true;
radioBtn.value = "newValue";

};


This doesn't change the text next to the radio button from "Button 1" to "newValue". I'm guessing I have to change the <label> tag instead since that's where actual text is located. Any thoughts?
×

Success!

Help @mark_yieh spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...