tags:

views:

106

answers:

3

Given this HTML - how can I extend my jQuery to make the text being displayed by the <input> elements appears "dimmed" or grayed out when I select the "Do Not Limit" radiobutton?

See the sample here: http://jquery.bluenose.ch/jquerydemo.html

<script type="text/javascript">
$(document).ready(function() {
  $("#rbnDontLimit").click(function() {
    $('.dcDetails').attr('checked', false).attr('disabled', true);
  });
});
</script>

<body class="contentBody">
  <input id="rbnDontLimit" type="radio" name="limitChoice">Do not Limit</input>

  <input id="months12" class="dcDetails" type="checkbox" name="choiceMonths">12 months</input>
</body>

Right now, clicking on the "Do Not Limit" button will properly disable the checkbox (thanks, gw, for all your help on this one!), but the text still appears identical as before.

Is there another clever jQuery / CSS trickery available to make that text appear dimmed??

Marc

+4  A: 
<script type="text/javascript">
$(document).ready(function() {
    $("#rbnDontLimit").click(function() {
     $('.dcDetails').attr('checked', false).attr('disabled', true).each(function(){
      $('label[for=' + $(this).attr('id')  + ']').css('color', 'gray');
     });
    });
});
</script>

Change gray to whatever dimmed-ish color you'd like.

Or you can go cleaner:

<script type="text/javascript">
$(document).ready(function() {
    $("#rbnDontLimit").click(function() {
     $('.dcDetails').attr('checked', false).attr('disabled', true).each(function(){
      $('label[for=' + $(this).attr('id')  + ']').addClass('disabled');
     });
    });
});
</script>

And add the CSS definition:

.disabled { color: gray; }
Josh Leitzel
Hmmm.... that doesn't seem to be doing anything at all :-( I even tried setting it to "red" just to be absolutely sure! :-) No change....
marc_s
Same with the "addClass" call - the text doesn't change appearance at all :-(
marc_s
Sorry, I didn't read your HTML fully. Adding a color attribute to a checkbox itself is pointless. You need to wrap the "12 months" text in a <label>. Like this: <label for="months12">12 months</label>. (Also, it should be <input />, not <input></input>)I've updated the code in my answer. Should work fine if you apply a <label> to the text as I explained.
Josh Leitzel
Yes, thanks - now it works! I just hope I can get my ASP.NET page to render something along those lines!! ;-)
marc_s
Glad to help. Good luck with the rest of your project!
Josh Leitzel
addClass('disabled') is the way to go
cherouvim
+2  A: 

Just toggle a class when you click the radio - one class sets the color to #333333 and the other to #CCCCCC

Jonathan Sampson
+1  A: 

If you're using ASP.NET controls, make sure you use the ClientID property of the button controls when writing your JavaScript. This will save you from having to deal with the mangled IDs within content placeholders.

Example ASP.NET markup in a ContentPlaceHolder:

<asp:RadioButton ID="rbnDontLimit" runat="server" />
<asp:CheckBox ID="chkWhatever" CssClass="dcDetails" runat="server" />
<asp:Label ID="lblWhatever" AssociatedControlID="chkWhatever">12 months</asp:Label>

Generated HTML:

<input id="ctl00_MainContent_rbnDontLimit" type="radio" name="ctl00$MainContent$rbnDontLimit" value="ctl00_MainContent_rbnDontLimit" />
<span class="dcDetails"><input id="ctl00_MainContent_chkWhatever" type="checkbox" name="ctl00$MainContent$chkWhatever" /></span>
<label for="ctl00_MainContent_chkWhatever" id="ctl00_MainContent_lblWhatever">12 months</label>

And for your JavaScript on the page:

$("#<%= rbnDontLimit.ClientID %>").click(function() {
    $('.dcDetails').attr('checked', false).attr('disabled', true).each(function(){
            $('label[for=' + $(this).attr('id')  + ']').addClass('disabled');
    });
});
// Using <%= rbnDontLimit.ClientID %> on your .aspx page will generate:
// "ctl00_MainContent_rbnDontLimit"
John Rasch
Yes, thanks, I plan to use that technique!
marc_s