HighTechTalks DotNet Forums  

how to get selectedvalue of radiobuttonlist in Javascript?

ASP.net Web Controls microsoft.public.dotnet.framework.aspnet.webcontrols


Discuss how to get selectedvalue of radiobuttonlist in Javascript? in the ASP.net Web Controls forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Arnold
 
Posts: n/a

Default how to get selectedvalue of radiobuttonlist in Javascript? - 11-12-2006 , 06:16 AM






Hi,

I need the SelectedValue of a radiobuttonlist in a Javascript variable. The
radiobuttonlist is only used in a form for inputting data into a database.
Before sending it to the database, i check the inputted values in
Javascript.
(By the way there is also a dropdownlist in the form, and i have no problem
with getting its SelectedValue with the same javascript code). With the
radiobuttonlist, i get "undefined" as value.

Thanks for helping me
Arnold

The radiobuttonlist is created in the code-behind like this:

Dim rb As RadioButtonList
Dim frm As HtmlForm = Me.FindControl("form1")
Dim rbl(2) As ListItem
rb = New RadioButtonList
rbl(1) = New ListItem("option 1", 1)
rb.Items.Add(rbl(1))
rbl(2) = New ListItem("option 2", 2).
rb.Items.Add(rbl(2))
rb.ID = "radio1"
frm.Controls.Add(rb)

The code in the aspx file:
<form id="form1" runat="server">
<input id="Sub1" type="button" value="submit" onclick="checkvalue()"/>
</form>
<script type="text/javascript">
var antw
function checkvalue()
{
antw=document.getElementById("radio1").value
alert(antw) // this gives: undefined
.....
.....
}
</script>





Reply With Quote
  #2  
Old   
Bob Barrows [MVP]
 
Posts: n/a

Default Re: how to get selectedvalue of radiobuttonlist in Javascript? - 11-12-2006 , 07:02 AM






Arnold wrote:
Quote:
Hi,

I need the SelectedValue of a radiobuttonlist in a Javascript
variable. The radiobuttonlist is only used in a form for inputting
data into a database. Before sending it to the database, i check the
inputted values in Javascript.
(By the way there is also a dropdownlist in the form, and i have no
problem with getting its SelectedValue with the same javascript
code). With the radiobuttonlist, i get "undefined" as value.

Thanks for helping me
Arnold

The radiobuttonlist is created in the code-behind like this:

Dim rb As RadioButtonList
Dim frm As HtmlForm = Me.FindControl("form1")
Dim rbl(2) As ListItem
rb = New RadioButtonList
rbl(1) = New ListItem("option 1", 1)
rb.Items.Add(rbl(1))
rbl(2) = New ListItem("option 2", 2).
rb.Items.Add(rbl(2))
rb.ID = "radio1"
frm.Controls.Add(rb)

The code in the aspx file:
form id="form1" runat="server"
input id="Sub1" type="button" value="submit" onclick="checkvalue()"/
/form
script type="text/javascript"
var antw
function checkvalue()
{
antw=document.getElementById("radio1").value
alert(antw) // this gives: undefined
....
....
}
/script

Run the page then View Source to see the radio buttons generated by the .Net
control. This will reveal their IDs at which point it should be a simple
matter to to use getElementById to get the value.

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"




Reply With Quote
  #3  
Old   
johns221b
 
Posts: n/a

Default RE: how to get selectedvalue of radiobuttonlist in Javascript? - 11-12-2006 , 07:13 AM





"Arnold" wrote:

Quote:
Hi,

I need the SelectedValue of a radiobuttonlist in a Javascript variable. The
radiobuttonlist is only used in a form for inputting data into a database.
Before sending it to the database, i check the inputted values in
Javascript.
(By the way there is also a dropdownlist in the form, and i have no problem
with getting its SelectedValue with the same javascript code). With the
radiobuttonlist, i get "undefined" as value.

Thanks for helping me
Arnold
RadionList value can be processed in server side before sending to database.
radio1.SelectedItem.Text
For javascript you will have to loop through the collection of radio
buttons and check whether its checked or not, then take cchecked value.

thanks,
John Chacko





Quote:
The radiobuttonlist is created in the code-behind like this:

Dim rb As RadioButtonList
Dim frm As HtmlForm = Me.FindControl("form1")
Dim rbl(2) As ListItem
rb = New RadioButtonList
rbl(1) = New ListItem("option 1", 1)
rb.Items.Add(rbl(1))
rbl(2) = New ListItem("option 2", 2).
rb.Items.Add(rbl(2))
rb.ID = "radio1"
frm.Controls.Add(rb)

The code in the aspx file:
form id="form1" runat="server"
input id="Sub1" type="button" value="submit" onclick="checkvalue()"/
/form
script type="text/javascript"
var antw
function checkvalue()
{
antw=document.getElementById("radio1").value
alert(antw) // this gives: undefined
.....
.....
}
/script






Reply With Quote
  #4  
Old   
Arnold
 
Posts: n/a

Default Re: how to get selectedvalue of radiobuttonlist in Javascript? - 11-12-2006 , 09:24 AM



Thanks to all

"Arnold" <ar (AT) nold (DOT) cv> schreef in bericht
news:OvZJRRlBHHA.3536 (AT) TK2MSFTNGP03 (DOT) phx.gbl...
Quote:
Hi,

I need the SelectedValue of a radiobuttonlist in a Javascript variable.
The radiobuttonlist is only used in a form for inputting data into a
database. Before sending it to the database, i check the inputted values
in Javascript.
(By the way there is also a dropdownlist in the form, and i have no
problem with getting its SelectedValue with the same javascript code).
With the radiobuttonlist, i get "undefined" as value.

Thanks for helping me
Arnold

The radiobuttonlist is created in the code-behind like this:

Dim rb As RadioButtonList
Dim frm As HtmlForm = Me.FindControl("form1")
Dim rbl(2) As ListItem
rb = New RadioButtonList
rbl(1) = New ListItem("option 1", 1)
rb.Items.Add(rbl(1))
rbl(2) = New ListItem("option 2", 2).
rb.Items.Add(rbl(2))
rb.ID = "radio1"
frm.Controls.Add(rb)

The code in the aspx file:
form id="form1" runat="server"
input id="Sub1" type="button" value="submit" onclick="checkvalue()"/
/form
script type="text/javascript"
var antw
function checkvalue()
{
antw=document.getElementById("radio1").value
alert(antw) // this gives: undefined
....
....
}
/script







Reply With Quote
  #5  
Old   
MikeS
 
Posts: n/a

Default Re: how to get selectedvalue of radiobuttonlist in Javascript? - 11-12-2006 , 09:35 AM



Or I guess you could change your script a little.

function checkvalue()
{
var col = document.getElementsByName("radio1")
for (i = 0 ; i < col.length ; i++) {
var el = col[i];
if (el.type == "radio" && el.checked) {
alert(el.value);
break;
}
}
}


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 - 2013, Jelsoft Enterprises Ltd.