Possible Duplicate:
Check of specific radio button is checked
I have these 2 radio buttons at the moment so that the user can decide whether they need postage included in the price or not:
<input type="radio" id="postageyes" name="postage" value="Yes" /> Yes
<input type="radio" id="postageno" name="postage" value="No" /> No
I need to use Jquery to check if the 'yes' radio button is checked, and if it is, do an append function. Could someone tell me how I'd do this please?
Thanks for any help
edit:
I've updated my code to this, but it's not working. Am I doing something wrong?
<script type='text/javascript'>
// <![CDATA[
jQuery(document).ready(function(){
$('input:radio[name="postage"]').change(function(){
if($(this).val() == 'Yes'){
alert("test");
}
});
});
// ]]>
</script>
Best Answer
Or, the above - again - using a little less superfluous jQuery:
Revised (improved-some) jQuery:
JS Fiddle demo.
And, further, a mild update (since I was editing to include Snippets as well as the JS Fiddle links), in order to wrap the
<input />
elements with<label>
s - allow for clicking the text to update the relevant<input />
- and changing the means of creating the content to append:JS Fiddle demo.
Also, if you only need to show content depending on which element is checked by the user, a slight update that will toggle visibility using an explicit show/hide:
And, finally, using just CSS:
JS Fiddle demo.
References:
:checked
selector.~
) combinator.appendTo()
.change()
.:checked
selector.filter()
.is()
.:radio
selector.remove()
.text()
.toggle()
.val()
.