I’m working on an html assignment using javascript and I need to have two radio buttons as well as a text box. When you click on one of the radio buttons it needs to display text inside the text box.
hmmm…looks like you haven’t done much of the assignment at all so far.
The onclick need to call a function that writes whatever is passed to the function into the textbox.
You haven’t specified where the text to go into the textbox will come from and your post implies the same text will be written regardless of which radio button is clicked
The onclick function needs to do the following.
get a reference to the textbox object
set the value of the object in 1) to whatever value was passed to the onclick function
Short of writing the code and doing the assignment for you, I can’t help much more.
Here’s an example function i wrote quickly, as Kalon said you need to refer the onclick event to a javascript function.
function insertData(txt){
var e = document.getElementsByTagName('input');
for(var i=0; i<e.length; i++){
if (e[i].name == 'textbox1'){
e[i].value = txt;
}
}
}
there you go - SgtLegend’s done the assignment for you
if you want you can cut down the code by removing the for loop by assigning an id to the textbox as well and using getElementById() instead of getElementsByTagName().
Thanks for the help guys. I figured I’d have to use a function kalon. Unfortunately, the class I’m doing this for has one of the WORST professors I’ve ever had we haven’t gone over anything other than a function that generates a random number. So yea, I’m pretty much expected to read online and figure out how to do it, then show that I know how.
Anyways, enough ranting.
SgtLegend, thanks a lot for the example. I tried to implement it though and I must be doing something wrong. This is what I tried:
<script type = "text/javascript" >
function insertData(txt){
var e = document.getElementsByTagName('input');
for(var i=0; i<e.length; i++){
if (e[i].name == 'textbox1'){
e[i].value = txt;
}
}
}
</script>
<form>
<input type="radio" name="yes" value="" onclick ="insertData(hi) "/> yes </br>
<input type="radio" name="no" value="" /> no </br>
<input type="text" name="textbox1" value=""> </br>
</form>
This however isn’t working. Is there something else I should have changed with your function? If not, can you explain where I’m going wrong
How do I assign an id to the textbox? Sorry, but I seriously feel like I’ve been dropped in the ocean and then asked to swim. So I don’t know a lot of the stuff I should know by the time I take on a project like this.
I actually was able to do most of my assignment (there were other parts not involving radio buttons) simply with that small piece of advice. I even made the function better, by allowing different buttons to pass it a textbox number and then it would output text into that textbox! (helpful as I have multiple buttons and text boxes)
Anyways, I did run into another problem. If I want to have a drop-down list is there a way to put an onclick value in them? I tried :
but that didn’t work at all. I’d imagine since each value isn’t a technical box I can’t add an onclick option? If not, could I write a function where, at the push of a button, it would check and see what value I had in the drop-down menu? If so, when I wrote the function would tests such as :
var e = document.getElementById(dropdown1);
if (e.value == red) {