Get RadioButtonList's selected value and index using Javascript

Hey,

I cannot figure out the way to get the selected value of a radiobuttonlist. Here is the code: 

function PageIsFilleOut(i_name, limitt)
{
        var temp = limitt - 1;
        var check_text_area = new Boolean(false);
        var index=1;
       
        for (index = 1; index <= temp; index++)
        {
            var name_t = i_name + index;  //I have  several radiobuttonlist named a1,a2, and so on. This line create a string variable that I can use in the next statement.


            var select_t = document.getElementById(name_t);
           
            var test1= confirm(tt.selectedIndex+'-----'+name_t);       //tt.selectedIndex shows undefifned but is checked
         }

Thanks.

Christian 

0
christian
1/14/2008 7:39:24 PM
asp.net.client-side 24353 articles. 2 followers. Follow

10 Replies
8090 Views

Similar Articles

[PageSpeed] 55

I set up this example, which may help:

<script type="text/javascript">

function OnRadioSelection(control)

{

var button = null;var selection = -1;

 

if (control)

{

for (var i=0; i < control.cells.length; i++)

{

button = document.getElementById(control.id +
"_" + i);

 

if (button && button.checked)

{

selection = i;

break;

}

}

}

 

if (selection > -1)

{

alert(
'Button selected was ' + selection + 1);

}

}

</script>

 And then created the following in the aspx page:

<asp:RadioButtonList ID="RadioButtonList1" runat="server" onclick="javascript: OnRadioSelection(this);">

<asp:ListItem>ListItem 1</asp:ListItem>

<asp:ListItem>ListItem 2</asp:ListItem>

<asp:ListItem>ListItem 3</asp:ListItem>

</asp:RadioButtonList></td>

 

This worked for me!

 

Hope this helps,

 

Jon


Hope this helps!



Jon

If I was able to help, please mark this post as Answer.
0
JByrd2007
1/14/2008 10:57:52 PM

 Hi Jon, small world, huh?  This is JD over in Elon's UR dept - Alex's old job.  I was googling a javascript problem and up came your post.  It works great in IE, but I tweaked it a tiny bit to get it working in Firefox as well.  My particular need was to get the selected value.

 

 (code edited, I found a bug after posting originally...)

 

function OnRadioSelection(control)
{
if (control)
{
var length;
if (control.cells)
{
length = control.cells.length;
}
else
{
length = control.rows.length;
}

for (var i = 0; i <= length; i++)
{
var item = document.getElementById(control.id + "_" + i);
if (item && item.checked)
{
//alert("Selected value: " + button.value);
model.selectedCategoryChanged(item.value);
break;
}
}
}
}
  
0
headchem
3/12/2008 7:13:17 PM

Try this:

<script type="text/JavaScript">
<!--
function readListItem()
{
 var elementRef = document.getElementById('<%= RadioButtonList1.ClientID %>');
 var inputElementArray = elementRef.getElementsByTagName('INPUT');

 for (var i=0; i<inputElementArray.length; i++)
 {
  alert('#' + i + ': ' + inputElementArray[i].checked);
 }

 return false;
}
// -->
</script>

NC...

0
NC01
3/12/2008 7:22:15 PM

Thanks for the elegant code NC01.  Here's my revised code based on what you just posted:

 

 

function OnRadioSelection(control)
{    
    var items = control.getElementsByTagName('INPUT');

    for (var i = 0; i < items.length; i++)
    {
        if (items[i].checked)
        {
            model.selectedCategoryChanged(items[i].value);
        }
    }
    
    return false;    
}
  
0
headchem
3/12/2008 7:43:01 PM

So I take it that you are attaching the event handler like this:
     RadioButtonList1.Attributes.Add("onclick", "OnRadioSelection(this);");

NC...

0
NC01
3/13/2008 11:03:32 AM

Yes, but I'm actually doing it in the aspx page by adding the attribute to my <asp:RadioButtonList> tag:

 

onclick="javascript: OnRadioSelection(this);"
  
0
headchem
3/13/2008 12:31:02 PM

That will also work, though I believe that you will get an error in the Designer: Could not find any attribute OnRadioSelection of RadioButtonList or something like that.

NC...

 

0
NC01
3/13/2008 1:03:33 PM

 Yeah, it gives me the green squigly in source view.  My personal preference is to keep any javascript-related code in the aspx page as much as possible.

-1
headchem
3/13/2008 1:15:34 PM

Following article is exactly answring your question (with asp.net and javascript code)

http://dotnetfunda.com/articles/article57.aspx

Thanks


--
Thanks with regards,
Sheo Narayan
http://www.dotnetfunda.com/profile/SheoNarayan.aspx

Please "Mark as Answer", if this helped.
0
SheoNarayan
5/8/2008 12:30:35 PM

SheoNarayan:

Following article is exactly answring your question (with asp.net and javascript code)

http://dotnetfunda.com/articles/article57.aspx

Thanks

Why bring up a post that is 2 months old? Also remember that this will only work with RadioButtonLists and not CheckBoxLists.

NC...

 

0
NC01
5/8/2008 12:57:26 PM
Reply:

Similar Artilces:

dropdownlist's selected index value is not printing the selected value
Here iS my code .Aspx Page <asp:DropDownList ID="ddlkeyword" runat="server" Width="204px" DataTextField="Form" DataValueField="Form" OnSelectedIndexChanged="ddlkeyword_SelectedIndexChanged" >   </asp:DropDownList> <asp:Button ID="Button1" runat="server" Text="Submit" Width="110px" OnClick="Button1_Click" /> my aspx.cs protected void Page_Load(object sender, EventArgs e) { fillddlkeyword(); }protected void Page_Init(object sender, EventArgs e) { ...

Get the Dropdownlist text and value using Javascript from client side
Hi, I am encountering a problem about how to get the Dropdownlist text and value using Javascript from client side. I am using C# in Visual Studio 2005 and Javasript . The example codes are like: ( In this example code, I would like to access the text and value of the DropDownListReports in the run() function of Javascript. How can I do that? Any ideas? Thanks a lot. ) protected void Page_Load(object sender, EventArgs e) {              if (!Page.IsPostBack)            &nb...

How to get Radiobuttonlist inside of webusercontrol Selected Value using Javascript
Hello, Can someone please help me with this? I can get the values if the list is not in a user control, but when it's in a web user control I can't. I also have a masterpage.   function geturl2() {          var rdolist = document.getElementsByName('<%= ((RadioButtonList)wc1.FindControl("RadioButtonList1")).ClientID %>');          if (rdolist[1].checked) {                  ...

How to get to know user's selection rectangle on client-side?
When user select a section of text on a webpage, the javascript can get to know that by document.selection. But how can I get to know exactly where the selection is. I mean, the rectangle which contains the selected text, just like the highlighted background of the selected text. Is there a way I can get its coordinates informations? I checked out the related DOM properties and method but get nothing interesting. Can anybody gives a hint?Thanks in advance! If you track the mousedown and mouseup on document.body, you can use the coordinates provided from the event(varies by browser).&n...

Can you change a linkbutton's text using client side javascript?
I have a linkbutton which opens a new window. When the user closes the new window, I want to change the text of the original linkbutton. The issue isn't the popup window since know how to use window.opener. The problem is that I don't know how to use javascript to change the inner text of the linkbutton which I know is rendered as anchor tags. Any suggestions? Hi, on the Page where the LinkButton is, you can output the client-side ID of the LinkButton with it's ClientID property assuming you have <asp:LinkButton ID="LinkButton1" runat="server" Text="Hello" />So you could ou...

Default value for Dropdownlist that get's it's value from Database.
Hi, I am using an Object Data Souce to retrieve the value of a Dropdownlist from a table on MS SQL server 2005. I want to make a default value appear on the Dropdownlist intially, should i enter the default value into the database table or is that a better way of doing it? Any suggestions will be very much appreciated. E  No need for database just set sekected value after databingind protected void Page_Load(object sender, EventArgs e) { DataBind(); ListItem defaultItem = new ListItem("Select a Person", "-1"); th...

does any one know how to get client side machine's DPI(Dots per Inch) Value?
does any one know how to get client side machine's DPI(Dots per Inch) Value? Isn't DPI for printing: Wikipedia: Dots per inch. Would getting the display resolution suffice?  I just want to know how to get client machines DPI's Value.  for the later printing purpose. JavaScript has no control over printers.Eric Pascarello.com | Twitter epascarello | LinkedIn Do you know any method ,we can achieve. Because we need to show and print the report. all the controls' position will be saved in mm unit. and when the user want to show the report in thei...

Invalid index error
Hi all. When I go to any of our users who use GW6.5 and click on the checklist folder I get error C00E which states the following fix: C00E Invalid index number Source: GroupWise engine; database services. Explanation: Invalid index number. Possible Cause: When converting a post office from 5.x to 6.x, the post office database (wphost.db) was missing in the post office. Action: Restore the wphost.db file to the post office. Although the conversion does not actually use the old wphost.db file, it does attempt to access a file by that name in order to rename it. ...

How to use client-side script(javascript) in DNA using .Net Remoting
Hi all,     It was known that SAO and Singleton object about .Net Remoting. When I Create an object in server side and hosting in IIS's virtual directory,I want to use javascript asynchronous invoking the remote object by HTTP channel, 80 port and SOAP formatter just like XMLHTTP invoking web service. My code as following, <script> objXmlDoc = new ActiveXObject("Msxml2.DOMDocument"); objHttp = new ActiveXObject("Msxml2.XMLHTTP"); strEnvelope = ?? objHttp.onreadystatechange = function() { ...... }   szUrl = "http://ServerNa...

How to get a Html.CheckBox 's Ischecked Value using FormMethod.Get?
How to get a Html.CheckBox 's Ischecked Value using  FormMethod.Get? When I use the FormMethod.Post Method, I can get the Html.CheckBox 's Ischecked Value by string ischeck = Request.Form.GetValues("TestCheckBox")[0]; But when I use the FormMethod.Get  Method, How can I get the Html.CheckBox 's Ischecked Value ?  Lord Jesus, Thank you for creating me and guiding me through everyday. Mike108 ASP.NET MVC Blog´╝Ühttp://mike108mvp.cnblogs.com You actually just... can't. The client's browser will only submit the checkbox' value when it has bee...

how do i get selectedValue of drop down list server side if i am populating it using client side javascript?
 Hi All.I am populating a drop down list client side using: The drop down list is populated based on a selection of another drop down list. ddlList.options[i] = new Option(1,2); as an exampleThe populating is done as i need it to. However, a button click event i am trying to get the selectedValue of the ddlList. However, it doesn't give me it. It only gives me the selectedValue of the drop down list on first rendering. Any ideas on how i can get it?   Could You paste some code? That would be helpful. Thanks S@ss (Shashi Shekhar Singh)My BlogPlease mark a post as ANSWER ...

Get Client system's directory/folder list using client script
hello to all, I want to know that is there any way by which i could retrieve client's harddisk information at client side.i mean to ask is, I want create a web based FTP so i need client systems directory at one side.i can retrieve it using webservice or ActiveX control or normal server control.i am restricted to use of ActiveX controls and webservice internally make call to webserver,server control also do the same. so i want to retrieve client information at client side only .   please update me. Thanks Uhh, you want his Social Security Number and credit card number too? Yo...

Getting DropDownlist's DataTextValue Selected Value Given the SelectedValue and/or Selected Item
In the vb.net codebehind, I need to extract something from the Text (displayed) value of dropdownlist's selected item and I have  the SelectedValuand and SelectedItem in hand. How can I do this?  FindControl? Item? row?   Thanks. Well, its easier than you might think. SelectedItem has a "text" property. Thats the selected text right there :)  If you need the Text of the selection, then you have to use the SelectedItem.Text property.  If you want the value (not the displayed text) the you can directly use the SelectedValue. dim sText as String = dropdown...

get the row element values using row index for dynamic dropdownlist using javascript
Friends,How to get the row element values using row index for dynamic dropdownlist using javascript ?Thanks in advance  I want creating dynamic dropdownlist  using javascript. I am working on deleting a row. function DeleteRow(r) { var i = r.parentNode.parentNode.rowIndex;document.getElementById("ctl_Contentmain_tbldynamic").deleteRow(i);var str = document.getElementByName("ddljobs");   alert(str); } Check out this. var str = document.getElementById('<%=ddljobs.ClientID%>');   Thanks,santosh_maharajaPlease mark as answer ...

JavaScript to get Selected RadioButtonList value
Hi all,I want to know and ask you guys something.I have a radiobuttonlist and 2 normal radio buttons in my asp.net web page. And I use javascript to control the clicking of these controls. I already asked and found the way of using javascript to control normal radio button and it worked fine.But when i used these function to control the radionbuttonlist, it didn't seem to work well. (It worked but seems to have a little issue) My flow is when i click on radiobuttonlist (I have 3 listitem in these radiobuttonlist), i will get the selected value. So, i need to know which one is checked/sel...

How to get return value or output value after insert new record (Scott's tutor used BLL)
I ve created a DAL and BLL following Scott s tutorials and I don't know how to get return value or output value after insert new record.In Stored Procudure: INSERT INTO .... RETURN SCOPE_IDENTITY()or : I declare out put variable: @newRecordId INT OUTPUTthen at the end SP: SET @newRecordId = SCOPE_IDENTITY()in the BLL, I have used addnew function following the Scott' tutor.But I don't know how to get newRecordId.Someone can help me,Thank a lot   Here's an example for you: ASPX <asp:detailsview id="dvSuppliers" runat="server" autogenerat...

How to get Radiobuttonlist's value at clientside?
I have one radiobuttonlist on my page and i want to perform action at clientside when radiobuttonlist's options are changed. I am getting 'undefined' when i alert at javascript. In my vb page i am writing like this :          rblVersion.Attributes.Add("onClick", "return checkVersion(this.value)") My javascript code is:        function checkVersion(version)         {             &nb...

How to use Client Side Scripting (Javascript) for RadioButtonLists?
Hi,  I have a two RadioButtonLists, rblParent and rblChild, each of which has has four radio buttons. All the buttons in rblChild are disabled at design time.   When the user selects an option in rblParent, I want to enable some of the radiobuttons in rblChild depending on the option selected by the User. I thought of using javascript, onClick event, but how do I do it for  <asp:RadioButtonList> controls? My aspx markup which fails when I build looks like this:   <asp:RadioButtonList ID="rblParent" runat="server" RepeatDirection=&quo...

Mozilla get's no style tag's from .net server.
I made a webform in web matrix, and it looks fine in IE. If I open the same link whit Mozilla, it looks like a disaster. When I look at the HTML source, there are no style tags as in IE. But when I save the HTML source from IE (the one whit style tags) and open it in Mozilla, it looks perfect. So the onley reason the webform looks wrong in Mozilla, is because the .net server detects Mozilla and not IE and sends a style less HTML source. Wy? Mozilla does support style tags. Is there a solution for this bug? Or is this a uh.. feature? ..Mivz Update, I have got some more info. It is ...

How can I get out of my Mozilla's browser the Winamp browser and get back the Google's or Mozilla's browser.
Please I have got to my browser ( I don't know how.) the Winamp browser he behave like virus stick in.But he doesn't has uninstalation section so I can't do anything to get him away from Mozilla's file. I'm not happy of Winamp's behave its like advertisement against a customer's will. Please is there any way to get him away from Mozilla?Please send me exact direction I have tried many ways but he is deeply inside the systeme so when I install new Mozilla he is always here ready to pester me. Thank you for an answer. Richard ...

Help! I cannot access the DataGrid's ItemTemplate's LinkButton's Text Atrribute 's Value!!!!!!!!!!!
I write a datagrid's ItemDataBound method as follows:   private void dgTable_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)  {   if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)   {    if(!isRole("Manager"))    {     LinkButton tmp = (LinkButton)e.Item.FindControl("lbPassed");     if(tmp.Text == "Passed")     {      ((LinkBu...

How To Get the Gridview's Field Value if the Column's Visible=False..?
Here is the scenario: My sql query returns Name, ID, SSNum. I use a gridview which has 3 columns Name, ID and SSNum (with SSNum is set visible=false) so the gridview only shows Name and ID. I need to use part of SSNum to form a NewUserID in the code behind. Im doing this by using the gvResult_OnRowDataBound. So far, if I set SSNum column's visible=false then I wont see this field on row data bound. Any suggestion as how to obtain this SSNum from the code behind?  you can set it as the datakey on the properties of the grid view and use this codethis.GridView1.DataKeys[this.Gri...

How to set DDL's selected value by datasource other than DDL's datasource
I have three TemplateFields in a Gridview. In each, the ItemTemplate has a label whose text value is bound to the GridView's DataSource. But in the EditItemTemplate the DropDownList is populated with a separate DataSource. This is a fairly common setup, so that wasn't too much to deal with. What I need to do is, when the GridView goes in to Edit mode and the DropDownList is populated and displayed I want the value that was in the bound label to be the default selected value in the DropDownList. As it currently is, the DropDownList renders with "Select..." as the defau...

getting datagrid's height client side?
hi alli've come across a bit of a snag... i need to find out the height of the datagrid client side as i'm utilizing cascading datagrids and need to dynamically change the height of the grids... i guess i'd need to utilize javascript for this, but i have no idea how to code js...anyone come across this before?thanks!:) put the datagrid in an HTML table and use the height of the table. greets,interwanderersee my blog at www.Interdevelopments.com and check the future of open mobile widget solutions at www.glowe.org...

Web resources about - Get RadioButtonList's selected value and index using Javascript - asp.net.client-side

Resources last updated: 2/14/2016 12:53:25 PM