473,426 Members | 1,428 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,426 software developers and data experts.

add a textbox when radio button is clicked ?

Hi all,

Is it possible in javascript to add a textbox when the last of three
radiobuttons is clicked ?

thanks !
Apr 3 '07 #1
5 15127
Kulgan said the following on 4/3/2007 5:27 PM:
Hi all,

Is it possible in javascript to add a textbox when the last of three
radiobuttons is clicked ?
Yes, if the browser supports adding it dynamically. What happens if the
third is clicked and then the first one is clicked? Does the textbox
stay or does it go?

It is easier to hide/show the textbox though.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Apr 3 '07 #2
What happens if the third is clicked and then the first one is clicked?
Does the textbox
stay or does it go?
If the first or the second radiobutton is clicked the textbox must disapear
again ...
It is easier to hide/show the textbox though.
That would be a great solution, but how to do it ?

thanks!
Apr 4 '07 #3
On Apr 4, 2:13 am, "Kulgan" <nos...@skynet.bewrote:
What happens if the third is clicked and then the first one is clicked?
Does the textbox
stay or does it go?

If the first or the second radiobutton is clicked the textbox must disapear
again ...
It is easier to hide/show the textbox though.

That would be a great solution, but how to do it ?

thanks!
You can start with this and fine tune as necessary:

<script type="text/javascript">
function checkIt(el) {
if (el.value == "other") {
document.getElementById('text').style.display = "block";
}
else {
document.getElementById('text').style.display = "none";
document.getElementById('who').value = '';
}
}
</script>

<input type="radio" name="radio" value="one"
onclick="checkIt(this);">One
<input type="radio" name="radio" value="two"
onclick="checkIt(this);">Two
<input type="radio" name="radio" value="other"
onclick="checkIt(this);">Other <br>
<div id="text" style="display:none;">Other: <input type="text"
id="who"/></div>

This puts the textfield in a hidden div. If one or two is checked, the
textbox is hidden and it's value is reset (so you don't accidently
submit a value, you could always change this behaviour as your server
side should be checking the value of "radio" for "other" before it
attempts to process a "who" value). If other is checked, the textfield
is displayed.

HTH.

Apr 4 '07 #4
"Tom Cole" <tc****@gmail.comwrote in message
news:11**********************@q75g2000hsh.googlegr oups.com...
On Apr 4, 2:13 am, "Kulgan" <nos...@skynet.bewrote:
What happens if the third is clicked and then the first one is clicked?
Does the textbox
stay or does it go?

If the first or the second radiobutton is clicked the textbox must disapear
again ...
It is easier to hide/show the textbox though.

That would be a great solution, but how to do it ?

thanks!

You can start with this and fine tune as necessary:

<script type="text/javascript">
function checkIt(el) {
if (el.value == "other") {
document.getElementById('text').style.display = "block";
}
else {
document.getElementById('text').style.display = "none";
document.getElementById('who').value = '';
}
}
</script>

<input type="radio" name="radio" value="one"
onclick="checkIt(this);">One
<input type="radio" name="radio" value="two"
onclick="checkIt(this);">Two
<input type="radio" name="radio" value="other"
onclick="checkIt(this);">Other <br>
<div id="text" style="display:none;">Other: <input type="text"
id="who"/></div>

This puts the textfield in a hidden div. If one or two is checked, the
textbox is hidden and it's value is reset (so you don't accidently
submit a value, you could always change this behaviour as your server
side should be checking the value of "radio" for "other" before it
attempts to process a "who" value). If other is checked, the textfield
is displayed.
I think you put a little too much into that example. It is far more appropriate to use
the native collections that exist via JavaScript. Hence my example:

/* CSS */

form input[name="text1"]
{
display: none;
}

/* SCRIPT */

<script type="text/javascript">
function checkIt(el) {
if (el.value == "other") {
document.forms['form1'].elements['text1'].style.display = "block";
}
else {
document.forms['form1'].elements['text1'].style.display = "none";
document.forms['form1'].elements['text1'].value = '';
}
}
</script>

/* MARKUP */

<form name="form1">
<input type="radio" name="radio" value="one" onclick="checkIt(this);">One
<input type="radio" name="radio" value="two" onclick="checkIt(this);">Two
<input type="radio" name="radio" value="other" onclick="checkIt(this);">Other<br>
<input type="text" name="text1" />
</form>

Basically all I did was replace your getElementByIds with the form collection.

Also, Internet Explorer may not like my use of attribute selectors. That can be solved by
using a class or inline style.

-Lost
Apr 5 '07 #5
Hi all,

Thanks for your answers !

I found an easy way to do this:

Here's the script that I used:
################################################## #####
<script type="text/javascript">
function Show( elemID )
{
var elem = document.getElementById( elemID );

elem.style.position = 'relative';
elem.style.left = '0px';
}

function Hide( elemID )
{
var elem = document.getElementById( elemID );

elem.style.position = 'absolute';
elem.style.left = '-4000px';
}
################################################## ########

In combination with this html / css it's working fine :
################################################## ########
<tr class="even-row" align="center" id="Q2T8">
<td><input type="radio" name="Q2_A_8" value="1" id="Q2_A_8_1"
onclick="Hide('extraQ')"/></td>
<td><input type="radio" name="Q2_A_8" value="2" id="Q2_A_8_2"
onclick="Hide('extraQ')"/></td>
<td><input type="radio" name="Q2_A_8" value="3" id="Q2_A_8_3"
onclick="Show('extraQ')"/></td>
<td><input type="radio" name="Q2_A_8" value="4" id="Q2_A_8_4"
onclick="Show('extraQ')"/></td>
</tr>

<tr id="extraQ" class="extraquestion">
<td colspan="5">
<br><b>Here's the textbox !</b><br>Lorem ipsum dolor sit amet,
consectetuer adipiscing elit. Praesent venenatis mollis leo. Aliquam eget
justo. Mauris quis tellus in urna imperdiet convallis. Quisque vel dolor et
risus dictum consequat. Nullam porttitor pretium elit.
<textarea name="Q10" rows="3" cols="75" wrap="virtual"></textarea><br><br>
<td>
</tr>
################################################## #######

thanks !
Apr 5 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Claire Osborne | last post by:
I have created a form at http://www.habitatforhumanity.org.uk/html/gv_indiv_apply.htm In the Medical Details section we need to ensure that if a radio button is clicked to indicate a health...
2
by: Bart Verbeke | last post by:
Hi, I have a form with 2 radio buttons (ACCEPT/REJECT). When the page is initially loaded, no textbox should be visible. When a user clicks the REJECT radio button, a textbox should...
1
by: sarj | last post by:
Hi.... I have a asp:textbox whose text needs to be set when a radio button is clicked. The text would correspond to the value of the particular. However, although the text is correctly set in...
11
by: bill | last post by:
I dynamically create buttons and associate them with an event using AddHandler. I want all the button events to fire at one time, when the page is posted, instead of when each button is clicked....
1
by: namanhvu | last post by:
Hi everyone, I'm trying to create a form where the radio button is automatically selected when the input text field beside it is clicked. I know I need to use "onClick" somewhere but I don't...
1
by: jw01 | last post by:
Im trying to write a java script in HTML in which when a particular radio button is selected, a textbox would appear write next to the selected item. e.g (RADIO BUTTON) fhd: (RADIO BUTTON) fdj:...
2
by: jw01 | last post by:
I changed my code...Now everytime i click a radio button, a text box appears...What i want is when i hit a particular radio button, the textbox should only appear next to that radio button. And if...
2
by: Peted | last post by:
I have 6 radio buttons in a column with a single line text box next to each rb. is there a general straightforward way to link a radio button to the textbox next to it. I want to be able to...
4
by: januincse | last post by:
There 2 radio buttons.when a radio is clicked textbox should appear,when another radio clicked the existing textbox should disappear and new textbox should be enabled.
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.