Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 1918 |  Welcome, Guest!   Register  Login
Home > Articles > JavaScript > How to get value from TextBox, RadioButtonList, DropDownList, CheckBox through JavaScript

How to get value from TextBox, RadioButtonList, DropDownList, CheckBox through JavaScript

6 vote(s)
Rating: 4 out of 5
Article posted by SheoNarayan on 5/8/2008 | Views: 397161 | Category: JavaScript | Level: Intermediate red flag


In this article I am going to show how to get value from asp.net controls like TextBox, RadioButtonList, DropDownList, CheckBoxes through JavaScript.

Introduction

In day to day life generally we need to get values from asp.net server controls through JavaScript while developing web applications. I found several questions on the internet for the same subject. In this article I am going to show how to get values from asp.net controls like TextBox, RadioButtonList, DropDownList, CheckBoxes.


This picture is the UI screenshots of the the code I am going to present in this article



Getting TextBox Value in JavaScript

.aspx page code
Following code will render a TextBox and a Button control as displayed in the picture above.

<table>
<tr>
<th colspan="2" align="left">
Getting TextBox Value in JavaScript:
</th>
</tr>
<tr>
<td>
<asp:TextBox ID="txt1" runat="server"></asp:TextBox>
</td>
<td>
<input type="button" value="Submit" onclick="GetTextBoxValue('<%= txt1.ClientID %>')" />
</td>
</tr>
</table>


Get video tutorials of hundreds of ASP.NET Tips and Tricks - http://www.itfunda.com/aspnet-how-to-tips-and-tricks/Show/50

JavaScript function
Following code is JavaScript function to get value from TextBox control.
// Get TextBox value

function GetTextBoxValue(id)
{
alert(document.getElementById(id).value);
}


Getting DropDownList/ListBox selected value

Following code will render a DropDown (Html select control) and a button as displayed in the picture above.
.aspx code
<table>

<tr>
<th colspan="2" align="left">
Getting DropDown/ListView Value
</th>
</tr>
<tr>
<td>
<asp:DropDownList ID="dropDown" runat="Server">
<asp:ListItem Text="Item 1" Value="1" Selected="True"></asp:ListItem>
<asp:ListItem Text="Item 2" Value="2"></asp:ListItem>
<asp:ListItem Text="Item 3" Value="3"></asp:ListItem>
</asp:DropDownList>
</td>
<td>
<input type="button" value="Submit" onclick="GetDropDownValue('<%= dropDown.ClientID %>')" />
</td>
</tr>
</table>

JavaScript Code
Following is the JavaScript function to get the value from DropDown control
// Get DropDown value

function GetDropDownValue(id)
{
alert(document.getElementById(id).value);
}


Getting RadioButtonList selected value

Following code will render RadioButtons and a button as displayed in the picture above.
.aspx code
<table>

<tr>
<th colspan="2" align="left">
Getting RadioButton Value
</th>
</tr>
<tr>
<td>
<asp:RadioButtonList ID="radiobuttonlist1" runat="Server" RepeatLayout="flow" RepeatDirection="horizontal">
<asp:ListItem Text="Radio 1" Value="1" Selected="True"></asp:ListItem>
<asp:ListItem Text="Radio 2" Value="2"></asp:ListItem>
<asp:ListItem Text="Radio 3" Value="3"></asp:ListItem>
</asp:RadioButtonList>
</td>
<td>
<input type="button" value="Submit" onclick="GetRadioButtonValue('<%= radiobuttonlist1.ClientID %>')" />
</td>
</tr>
</table>

JavaScript Code
Following is the JavaScript function to get the selected value from RadioButtons
// Get radio button list value

function GetRadioButtonValue(id)
{
var radio = document.getElementsByName(id);
for (var ii = 0; ii < radio.length; ii++)
{
if (radio[ii].checked)
alert(radio[ii].value);
}
}


Getting CheckBox checked status

Following code will render a checkbox and a button as displayed in the picture above.
.aspx code
<table>

<tr>
<th colspan="2" align="left">
Getting Checkbox Value
</th>
</tr>
<tr>
<td>
<asp:CheckBox runat="server" ID="checkBox1" Text="Check Box" />
</td>
<td>
<input type="button" value="Submit" onclick="GetCheckBoxValue('<%= checkBox1.ClientID %>')" />
</td>
</tr>
</table>

JavaScript Code
Following is the JavaScript function to get value from a Checkbox.
 // Get Checkbox checked status

function GetCheckBoxValue(id)
{
alert(document.getElementById(id).checked);
}


Show/Hide Text using

Following code will render three buttons and a table element having some text as displayed in the picture above.
.aspx code

<b>Show/Hide display text</b><br />

<input type="button" value="Toggle Display Following Text" onclick="ToggleFollowingText('table1')" />
<input type="button" value="Show Only" onclick="ShowOrHide('show', 'table1')" />
<input type="button" value="Hide Only" onclick="ShowOrHide('hide', 'table1')" />
<table id="table1">
<tr>
<td style="background-color:Aqua">
This is my hidden text, You can play with it by clicking above button.
</td>
</tr>
</table>


JavaScript Code
Following is the JavaScript function to toggle display the table and show and hide element the table.


// Show/Hide element
function ToggleFollowingText(id)
{
document.getElementById(id).style.display == '' ? document.getElementById(id).style.display = 'none' : document.getElementById(id).style.display = '';

}

// Either show or hide element
function ShowOrHide(condition, id)
{
if (condition == 'show')
document.getElementById(id).style.display = '';
else if (condition == 'hide')
document.getElementById(id).style.display = 'none';

}


Hope this practical example will help someone.

Thanks

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

About Sheo Narayan

Experience:8 year(s)
Home page:http://www.snarayan.com
Member since:Tuesday, July 08, 2008
Level:HonoraryPlatinum
Status: [Microsoft_MVP] [Administrator]
Biography:Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001.

Connect me on Facebook | Twitter | LinkedIn | Blog

 Responses
Posted by: Phanisri | Posted on: 18 Aug 2009 02:35:13 AM

Hi
good article..
can you please help me?
master page and content page, external js file concept how to access the server controls in .js file.
Please help me...

Posted by: SheoNarayan | Posted on: 18 Aug 2009 07:04:37 PM

If you mean you want to use the value of Server control into .js file you can do that in following way.

In the .aspx page write
var formElement = '<%=YourServerControl.ClientID %>'

In the .js file use formElement variable and this should work.

Thanks

Posted by: Nainu | Posted on: 07 Apr 2011 12:31:04 AM | Points: 25

Hi Sir
I have a problem in selecting one checkbox at a time among three checkboxes in the grid columns.
I have written the javascript function as below.
if (col.Index == 4) {
cell04 = oRow.getCell(4);
cell05 = oRow.getCell(5);
cell06 = oRow.getCell(6);
if (cell04.getValue() == true) {
cell05.setValue(false);
if (cell06 != null) {
if (cell06.getValue() == true) {
cell06.setValue(false);
}
}
}
else if (cell05.getValue() == false && cell06.getValue() == false) {
if (cell04.getValue() == false) {
cell04.setValue(true)
}
}
}
else if (col.Index == 5) {
cell04 = oRow.getCell(4);
cell05 = oRow.getCell(5);
cell06 = oRow.getCell(6);

if (cell05.getValue() == true) {
cell04.setValue(false);
if (cell06 != null) {
if (cell06.getValue() == true) {
cell06.setValue(false);
}
}
}
else if (cell04.getValue() == false && cell06.getValue() == false) {
if (cell05.getValue() == false) {
cell05.setValue(true)
}
}

}
else if (col.Index == 6) {
cell04 = oRow.getCell(4);
cell05 = oRow.getCell(5);
cell06 = oRow.getCell(6);

if (cell06.getValue() == true) {
if (cell04.getValue() == true) {
cell04.setValue(false)
}
if (cell05.getValue() == true) {
cell05.setValue(false)
}

}
else if (cell04.getValue() == false && cell06.getValue() == false) {
cell04.setValue(true)
}
}
}
problem & Requirement:Firsttime it behaves properly.But later If I have ticked cell 06 and now I want to check cell 05 it is not selecting cell 05.Instead it is selecting cell04 and then on next click on cell 05 selects 05.Anyway by default cell 04 will be checked always and it will be changed upon selection.(at a time one should be selected).
Please suggest me on this regarding.

Posted by: Susanthampy | Posted on: 31 May 2011 08:34:41 AM | Points: 25

good....

Posted by: Mourya | Posted on: 10 Sep 2011 03:50:22 AM | Points: 25

@Nainu,

what is your actual requirement , ellobrate it in detail.
it wil be easier for all to help you out.
if possible post a screen shot of you gridview.

Thanks
Mourya

Posted by: Varung | Posted on: 05 Dec 2011 03:15:50 AM | Points: 25

we can use OnClientClick event for server control button to call javascript function if am not wrong.....
example:
<asp:Button ID="btn" runat="server" OnClientClick ="javascript:functionname/>

an right?????

>> Write Response - Respond to this post and get points
Related Posts

This article explains the use of execCommand method used in Javascript that is used to execute a command in JavaScript.

Prototype is used to define classes or objects in JavaScript. Classes or objects are building blocks of Object Oriented Programming.

We can access the check box in grid control in client side using java script

I am going to explain the simple GridView Client Side Validation instead of server side validation controls.

To check the IE 8.0 Version after install in server

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2012 7:22:45 AM