Home › Forums › JavaScript › [Solved] OnCheck Event?
- This topic is empty.
-
AuthorPosts
-
November 15, 2010 at 3:40 pm #30694realphParticipant
Hey there,
I’ve got a simple form set up with two buttons (Buying & Renting), when clicked they call their javascript and show their respective prices (Buying Price, Renting Price) which are obviously different.
The problem is that when I reload the page and the Rental radio button is checked, it doesn’t show the “Renting Prices”, and I’m having to re-click the Renting button for it to call the javascript and display the correct pricing.
Is there such thing as an onCheck event or something similar that would call the javascript when the radio button is checked rather than clicked?
Here’s what I’m working with:
checked="checked"/>Buying
/>
Renting
Thanks in advance!
November 15, 2010 at 4:50 pm #75424Chris CoyierKeymasterYou’ll have to wait for the DOM ready, then check the value of each radio button and figure out which is checked, and fire off the proper JavaScript to make it happen. This isn’t going to happen with inline JavaScript as you have there, but it’s doable and the correct way to handle it.
November 15, 2010 at 5:35 pm #75409realphParticipantSorry Chris but I’m a bit confused now, how would I do something like that? Any help will be appreciated.
November 15, 2010 at 6:26 pm #75410Chris CoyierKeymasterIf you can use jQuery…
$(function() {
if ($("input[name=RentalPeriod]:checked").val()) configureprices('buy');
});November 15, 2010 at 7:35 pm #75416realphParticipantThat seems to work. How would I add an else statement onto that bit of code?
IF the checked rental period has a value of 0, display the ‘buy’ prices. ELSE display the ‘rent’ prices.
Sorry, I’m extremely unfamiliar with jQuery.
November 15, 2010 at 8:51 pm #75417Chris CoyierKeymasterThe conditional logic part isn’t “jQuery”, but this is how you’d do that:
$(function() {
if ($("input[name=RentalPeriod]:checked").val()) {
configureprices('buy');
} else {
// whatever
}
});Normally I use the braces even for just an IF statement anyway, but if you are just doing one line as I was above, you can skip them.
November 15, 2010 at 9:35 pm #75404realphParticipantSorry to be a bother Chris, but is there a way to add a value to that IF statement? At the moment whenever either one of the RentalPeriod radio buttons are checked it’s calling the ‘buy’ JavaScript every time.
That’s fine for the Buying button, but I need it to call the ‘rent’ script when the Rental radio button is checked.
Hope that made sense, and thanks for all the help – you’re a star!
November 16, 2010 at 11:47 am #75306realphParticipantI managed to solve the problem with this:
$(function() {if ($("input[name=RentalPeriod]:checked").val() == '3') {
configureprices('rent');
} else {// whatever
}
});
Thanks a bunch Chris! -
AuthorPosts
- The forum ‘JavaScript’ is closed to new topics and replies.