Hi Nick,
You're close!
The first thing you might want change is your selector. You can use
the common CSS notation for selecting an ID.
So, $("input[id='plan']") can simply be $('#plan')
This will be more efficient, because jQuery will look for a single
element with an ID of "plan." Otherwise, it would look for all
elements with tag name "input" and then check to see which one had an
id of "plan."
Then, you can use the jQuery custom selector ":checked" to see if the
radio button is checked.
if ( $('#plan').is(':checked') ) {
// do your thing
}
Hope that helps.
--Karl
_________________
Karl Swedberg
www.englishrules.com
www.learningjquery.com
On Dec 15, 2007, at 3:24 AM, Nick Bourgeois wrote:
Hey all,
This is my first post to the community. I'm a total jQuery n00b,
and I'm struggling with conditional statements & jQuery. Here's
what I'm trying to do:
The page is a form that uses the same template for both add and edit
modes. On page load, jQuery should detect whether or not a choice
from a radio button selection is checked. Here is the relevant HTML:
<label for="plan"><input type="radio" id="plan" name="coverage"
value="Healthcare Plan" checked="checked" /> My <strong>healthcare
plan</strong> includes prescription drug benefits.</label>
<label for="medicare"><input type="radio" id="medicare"
name="coverage" value="Medicare Part D or Medicaid" /> I am enrolled
in either <strong>Medicare Part D</strong> or <strong>Medicaid</
strong>.</label>
<label for="hsa"><input type="radio" id="hsa" name="coverage"
value="Health Savings Account" /> I have a <strong>Health Savings
Account</strong> (HSA) and otherwise plan to pay retail price.</label>
If the first choice is selected on page load, do nothing. If the
other two are selected on page load, show/hide certain fields. I
have the show/hide stuff working, but I'm struggling with the
conditional stuff.
I'm trying to do something like ...
if ($("input[id='plan']").attr('checked').is(false)) {
// show/hide
}
... but, y'know, something that works. ;)
Any help would be much appreciated.
TIA,
NIck