Wrox Programmer Forums
|
ASP.NET 1.x and 2.0 Application Design Application design with ASP.NET 1.0, 1.1, and 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.x and 2.0 Application Design section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 14th, 2008, 09:57 AM
Authorized User
 
Join Date: Mar 2008
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to myself.panku
Default set text to checkbox

hello firnds..I am new in web development..pls tell me how to set the chkbox's text using java script

is there something so?

var objchk1 = document.getElementById("chk1");
objchk1.text = 'text';

thanks!!

__________________
Thanks
-Pankaj Bahuguna
 
Old April 14th, 2008, 12:45 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Nope; a check box doesn't have a text property in HTML.

To see how .NET does it, create a brand new web page, add a CheckBox control to the page, set its Text property and then request the page in the browser. The final HTML of the page shows you something like this:

<input id="CheckBox1" type="checkbox" name="CheckBox1" />
<label for="CheckBox1">My Old Text</label>

As you can see, it's quite difficult to target the text, as it's stored in a <label /> element without an explicit ID. I think the following would work around this, and find the right <label />:
Code:
var allLabels = document.getElementsByTagName("label");
for (var i = 0; i < allLabels.length; i++)
{
  if (allLabels[i].htmlFor == "CheckBox1")
  {
      allLabels[i].innerText = 'My New Text';
  }
}
Since this is very fragile code, and really depends on a fixed naming scheme, you may be better off leaving the Text property of the CheckBox control empty, and manually create a <label /> for it.

Hope this helps,

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
 
Old April 14th, 2008, 12:57 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

BTW: alternatively, but equally fragile, you could try this:

document.getElementById('CheckBox1').nextSibling.i nnerText = ''My New Text';

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 5 read text & checkbox: a stopper! zeitguy BOOK: Beginning Ruby on Rails 5 April 1st, 2008 10:37 PM
Set Text to checkbox myself.panku ASP.NET 2.0 Basics 1 March 17th, 2008 05:33 AM
Beginning ASP 3.0: changing text field to checkbox AlanM Classic ASP Basics 0 July 1st, 2007 03:36 AM
Can't set text property of dynamic text box sreerajrs ASP.NET 1.0 and 1.1 Professional 1 May 31st, 2006 09:39 PM
set text color larry Javascript How-To 9 July 8th, 2004 01:01 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.