Grow your CSS skills. Land your dream job.

[Solved] OnCheck Event?

  • # November 15, 2010 at 3:40 pm

    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
    < ?php if($_GET == "3") { echo("checked"); } ?> />
    Renting

    Thanks in advance!

    # November 15, 2010 at 4:50 pm

    You’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

    Sorry 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

    If you can use jQuery…

    $(function() {

    if ($("input[name=RentalPeriod]:checked").val()) configureprices('buy');

    });
    # November 15, 2010 at 7:35 pm

    That 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

    The 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

    Sorry 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

    I managed to solve the problem with this:


    $(function() {

    if ($("input[name=RentalPeriod]:checked").val() == '3') {
    configureprices('rent');
    } else {

    // whatever

    }

    });

    Thanks a bunch Chris!

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".