HighDots Forums  

getting value of radio buttons

JavaScript discussion (multi-lingual) JavaScript discussion (alt.comp.lang.javascript)


Discuss getting value of radio buttons in the JavaScript discussion (multi-lingual) forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Tony Rice
 
Posts: n/a

Default getting value of radio buttons - 04-03-2006 , 06:58 PM






I'm trying to come up with some Javascript that will display a text field
if a certain value is selected in a related radio button group. Something
like this:

<script type="text/javascript">

function showfield(id){
var sourceid='type_'+id;
var targetid='update_'+id;

var value=getRadioValue(sourceid);

if (value == 'c'){
document.getElementById(targetid).style.display = 'block';
}else{
document.getElementById(targetid).style.display = 'none';
}

}

function getRadioValue(obj){
if (!obj){
return;
}
for(var i=0;i<obj.length;i++){
if (obj[i].checked){
return(obj[i].value);
break;
}
}
return "unknown";
}

<script>

<input name="type_1" id="type_1" type="radio" value="a"
onChange="showField('1')">a
<input name="type_1" id="type_1" type="radio" value="b"
onChange="showField('1')>b
<input name="type_1" id="type_1" type="radio" value="c"
onChange="showField('1')>c

<div style="display: block;" id="update_1"><textarea name="update_1">
</textarea></div>



I'd like to be able to have the textarea appear only when choice 'c' is
selected. My problem is in the getRadioValue function. obj.length
doesn't appear to be returning anything, ever. Is the problem in how I'm
getting the object? Or somewhere else?

Reply With Quote
  #2  
Old   
Tony Rice
 
Posts: n/a

Default Re: getting value of radio buttons - 04-03-2006 , 07:44 PM






Randy Webb <HikksNotAtHome (AT) aol (DOT) com> wrote in
news:ILadnfBQ9eTgXazZ4p2dnA (AT) comcast (DOT) com:

Quote:
URL: http://www.jibbering.com/faq/faq_not...ess.html#faBut

Will show you how to get the value of a Radio Button.
That's pretty much what I'm doing, but with a named id. Not working
though.


Reply With Quote
  #3  
Old   
Randy Webb
 
Posts: n/a

Default Re: getting value of radio buttons - 04-03-2006 , 07:45 PM



Tony Rice said the following on 4/3/2006 7:58 PM:
Quote:
I'm trying to come up with some Javascript that will display a text field
if a certain value is selected in a related radio button group. Something
like this:
<URL: http://www.jibbering.com/faq/faq_not...ess.html#faBut >

Will show you how to get the value of a Radio Button.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/


Reply With Quote
  #4  
Old   
Randy Webb
 
Posts: n/a

Default Re: getting value of radio buttons - 04-03-2006 , 07:51 PM



Tony Rice said the following on 4/3/2006 8:44 PM:
Quote:
Randy Webb <HikksNotAtHome (AT) aol (DOT) com> wrote in
news:ILadnfBQ9eTgXazZ4p2dnA (AT) comcast (DOT) com:

URL: http://www.jibbering.com/faq/faq_not...ess.html#faBut

Will show you how to get the value of a Radio Button.

That's pretty much what I'm doing, but with a named id. Not working
though.
Give them a name.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/


Reply With Quote
  #5  
Old   
Tony Rice
 
Posts: n/a

Default Re: getting value of radio buttons - 04-03-2006 , 08:11 PM



Randy Webb <HikksNotAtHome (AT) aol (DOT) com> wrote in
news:ILadne1Q9eQgXKzZ4p2dnA (AT) comcast (DOT) com:

Quote:
Tony Rice said the following on 4/3/2006 8:44 PM:
Randy Webb <HikksNotAtHome (AT) aol (DOT) com> wrote in
news:ILadnfBQ9eTgXazZ4p2dnA (AT) comcast (DOT) com:

URL: http://www.jibbering.com/faq/faq_not...ess.html#faBut

Will show you how to get the value of a Radio Button.

That's pretty much what I'm doing, but with a named id. Not working
though.

Give them a name.
I'm using document.getElementById, like so:

<script type="text/javascript">

function showfield(id){
var sourceid='type_'+id;
var targetid='update_'+id;

var value=getRadioValue(sourceid);

if (value == 'c'){
document.getElementById(targetid).style.display = 'block';
}else{
document.getElementById(targetid).style.display = 'none';
}

}

function getRadioValue(obj){
if (!obj){
return;
}
for(var i=0;i<obj.length;i++){
if (obj[i].checked){
return(obj[i].value);
break;
}
}
return "unknown";
}

<script>

<input name="type_1" id="type_1" type="radio" value="a"
onChange="showField('1')">a
<input name="type_1" id="type_1" type="radio" value="b"
onChange="showField('1')>b
<input name="type_1" id="type_1" type="radio" value="c"
onChange="showField('1')>c

<div style="display: block;" id="update_1"><textarea name="update_1">
</textarea></div>


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.