Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript [Solved] OnCheck Event?

  • This topic is empty.
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #30694
    realph
    Participant

    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!

    #75424
    Chris Coyier
    Keymaster

    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.

    #75409
    realph
    Participant

    Sorry Chris but I’m a bit confused now, how would I do something like that? Any help will be appreciated.

    #75410
    Chris Coyier
    Keymaster

    If you can use jQuery…

    $(function() {

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

    });
    #75416
    realph
    Participant

    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.

    #75417
    Chris Coyier
    Keymaster

    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.

    #75404
    realph
    Participant

    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!

    #75306
    realph
    Participant

    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)
  • The forum ‘JavaScript’ is closed to new topics and replies.