Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Web Controls / March 2008

Tip: Looking for answers? Try searching our database.

Having a checkbox make something available when it's checked.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mufasa - 25 Mar 2008 22:49 GMT
I have a check box with javascript code attached to it that will allow me to
enable an 'accept' butten when the person has checked the box. The code is:

input type="checkbox" name="checkbox"
onclick="javascript:document.getElementById('ctl00$MainPage$btnToCheckout').disabled=!this.checked;
return true; " />I

The code works fine in IE - the button becomes enabled when the check box is
clicked. But if Firefox it doesn't work.

Any thoughts?

TIA - Jeff.
David R. Longnecker - 26 Mar 2008 15:39 GMT
Jeff-

The Ctl00$... isn't necessary (as far as I know) on either of the browsers;
that's the control hierarchy generated by .NET.  getElementById should be
able to find the control simply by it's Id attribute.

   <div>
       <input type="checkbox"
           onclick="javascript:document.getElementById('btnToCheckOut').disabled=!this.checked;
return true;" />Accept?
       <button id="btnToCheckOut" disabled="disabled">Checkout</button>
   </div>

Tested in FF2, IE7, and Safari and works just fine.

HTH.

-dl

--
David R. Longnecker
http://blog.tiredstudent.com

> I have a check box with javascript code attached to it that will allow
> me to enable an 'accept' butten when the person has checked the box.
[quoted text clipped - 10 lines]
>
> TIA - Jeff.
Mufasa - 26 Mar 2008 17:50 GMT
That works fine except that I'm using an ASP:Button with code behind it.
That's why the funny name.

Any thoughts on getting this to work with ASP controls?

TIA - Jeff.

> Jeff-
>
[quoted text clipped - 33 lines]
>>
>> TIA - Jeff.
David R. Longnecker - 26 Mar 2008 20:24 GMT
Ahh, you are correct--wow, that's really annoying.  I tend to use HTML controls
wherever possible, so I've honestly never ran across this.

Okay, here's what I came up with.  This is more of a snafu battle between
FF and IE--out of all of them, Safari seems the most forgiving. Hah.  I tested
in FF2, FF7, and Safari.  So far, so good.

Anyway, I extracted this into a method, but I'll show both inline and the
method.

Html (all of it):

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
   <title>Untitled Page</title>
<script language="javascript" type="text/javascript">
function toggleButton(checkedValue)
{
   // You can remove the alerts; they were for testing!
   var button = document.getElementById('btnToCheckOut');
   alert("currently disabled: " + button.disabled);
   alert("checked value: " + checkedValue.toString());
   button.disabled = checkedValue;
   alert("now disabled: " + button.disabled);
   return true;
}

</script>
</head>
<body>
   <form id="form1" runat="server">
   <div>
       <input type="checkbox"
           onclick="javascript:toggleButton(!this.checked);" />Accept?
     
       <asp:Button runat="server" ID="btnToCheckOut" Enabled="false" Text="Checkout"
/>
   </div>
   </form>
</body>
</html>

and here's inline without the helper method:

<input type="checkbox"
onclick="javascript: var button = document.getElementById('btnToCheckOut');
button.disabled = !this.checked; return true;" />

That's modifying that <asp:Button> object.  From what I can tell, IE doesn't
care, but FireFox wants the attribute assignment separated out from the getElementById
call.  I'm sure Google could tell why... perhaps it has something to do with
the page loading order of how the controls hit the page.

HTH!

-dl

--
David R. Longnecker
http://blog.tiredstudent.com

> That works fine except that I'm using an ASP:Button with code behind
> it. That's why the funny name.
[quoted text clipped - 40 lines]
>>>
>>> TIA - Jeff.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.