0

This is one of my first javascripts but this is what I am trying to do:

  • If javascript is disabled then have the paypal radio button selected and hide the option to change to credit card.

  • If javascript is enabled then the credit card option is enabled by default and you also have the option to pick paypal.

  • If the creditcard option is selected on the radio, the relevant fields are shown.

This is my attempt of the code from the information I have read online. Can anyone help me see why it isn't working?

http://jsfiddle.net/spadez/PceBr/4/

// IF JAVASCRIPT ENABLED...

// select card option by default
$('input:radio[name="creditcard"]').prop("checked", true);
// unhide payment options
$('#paymentchoice').removeClass('hidden');
// unhide card options
$('input:radio[name="creditcard"]').change(
if ($(this).is(':checked') {
    $('#card').removeClass('hidden');
}
});
4
  • I believe you should check out this answer Commented Jul 12, 2013 at 13:00
  • If you work with radio buttons, they should use the same name Commented Jul 12, 2013 at 13:00
  • 1
    True, but apparently people do it, and it stops my card payment system working so I want to accommodate for these people. Commented Jul 12, 2013 at 13:00
  • 2
    @JeffNoel you never know Commented Jul 12, 2013 at 13:00

3 Answers 3

1

You may solve it like this:

HTML

I changed the name of the radios to group them:

Creditcard <input type="radio" name="payment_method" value="creditcard">
Paypal <input type="radio" name="payment_method" value="paypal" checked>

JavaScript

var options = $('#card');
var method = $('input[name=payment_method]');

method.change(function() {
    if ('creditcard' == $(this).val() ) {
        options.show();
    } else {
        options.hide();
    }
});

$('input[value=creditcard]').prop('checked', true);
options.show();

Demo

Try before buy

Sign up to request clarification or add additional context in comments.

Comments

1

Try to set the same name attribute to your radiobuttons. Also, delete the class="hidden" given to your #card DOMElement, since you want the credit card option to be selected at first (seeing your JavaScript):

HTML

<div id="paymentchoice">Creditcard
    <input type="radio" name="paymentProcessor" id="creditcard" value="creditcard">Paypal</input>
    <input type="radio" name="paymentProcessor" id="paypal" value="paypal" checked="checked" />
</div>
<div id="card" >
    <input type="text" name="cardname">
    <input type="text" name="cardnum">
    <input type="text" name="cardcode">
    <select name="month">
        <option value="01">01</option>
        <option value="02">02</option>
        <option value="03">03</option>
        <option value="04">04</option>
    </select>
    <select name="year">
        <option value="volvo">Volvo</option>
        <option value="saab">Saab</option>
        <option value="fiat">Fiat</option>
        <option value="audi">Audi</option>
    </select>
</div>
<input type="submit" value="Submit">

JavaScript/jQuery

// IF JAVASCRIPT ENABLED...

/*Add the change() function FIRST, since you set the creditcard as selected option just after that */

// unhide card options
$('input[name="paymentProcessor"]').change(function () {
    if ($('#creditcard').is(':checked')) $('#card').removeClass('hidden');
    else $('#card').addClass('hidden');
});

// select card option by default
$('#creditcard').prop("checked", true);

// unhide payment options
$('#paymentchoice').removeClass('hidden');

Live Demo

Comments

1

Try this:

<style type="text/css">
.hidden{
    display:none;
}
</style>


<script type="javascript">
    $('input[name=payment]').change(function() {
    if($(this).val() == "creditcard"){
        $('#card').show();
    }else{
         $('#card').hide();
    }
});
</script>


<noscript>
<style type="text/css">
    .creditcard {display:none;}
</style>  
</noscript>

<div id="paymentchoice">
    <span class="creditcard">Creditcard
    <input type="radio" name="payment" value="creditcard"></span>
        Paypal<input type="radio" name="payment" value="paypal" checked>
</div>

<div id="card" class="hidden">
    <input type="text" name="cardname">
    <input type="text" name="cardnum">
    <input type="text" name="cardcode">
    <select name="month">
        <option value="01">01</option>
        <option value="02">02</option>
        <option value="03">03</option>
        <option value="04">04</option>
    </select>
    <select name="year">
        <option value="volvo">Volvo</option>
        <option value="saab">Saab</option>
        <option value="fiat">Fiat</option>
        <option value="audi">Audi</option>
    </select>
</div>
<input type="submit" value="Submit">

Or this:

http://jsfiddle.net/sbml/PceBr/10/

2 Comments

A little disadvantage of this is, that you'll always have CSS code within your HTML file. When changes need to be done you may have to edit three parts (CSS, JavaScript and HTML) instead of only two (CSS or JavaScript).
@insertusernamehere Yes you are right, he could apply the class hidden to span creditcard and in javascript in top $('.creditcard').show();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.