5

I have a shopping cart checkout page and I'm trying to add a "gift" option. What needs to happen: Once the checkbox is selected for "Send Order As Gift", a value needs to be assigned to a hidden input field so that the information is moved onto the confirmation page and various receipts.

HTML:

<h3>Send Order As Gift</h3>
<ul>
    <li class="fc_row fc_gift"><label for="gift" class="fc_pre">This is a gift, 
    please do not include a receipt.</label>
    <input type="checkbox" name="gift" id="gift" class="checkbox" value="" />
    <input type="hidden" name="Gift" id="gift-true" value="" /></li>
    <script type="text/javascript">
    $(document).ready(function(){
        $("#gift-true").input( $('#edit-checkbox-id').is(':checked').val() + "Yes"); 
    });​                
    </script>
    </li>
    <li class="fc_row fc_gift_message"><label for="Gift Message">Include a message 
    (limit 100 characters):</label>
    <textarea name="Gift Message" cols="50" rows="3" maxlength="100"></textarea>
    </li>
</ul>

4 Answers 4

11

You have to update your hidden field whenever the checkbox is changed:

$(function(){
    $('#gift').change(function() {
        $("#gift-true").val(($(this).is(':checked')) ? "yes" : "no");
    });
});
Sign up to request clarification or add additional context in comments.

3 Comments

Just a copy from the question above. $('#gift') should do it.
Great! This worked perfectly for me. I changed one thing (let me know if you foresee an issue with this): $("#gift-true").val(($(this).is(':checked')) ? "yes" : ""); I need "no" to be blank, because if there is any value in the input it moves to the receipt and I don't want shoppers seeing "Gift: no" since there is no need.
Fine. There shouldn't be any issues using "" instead of "no".
1

Use jQuery .val() to set the value of the input element. jquery .is() returns a boolean value, so you will have to check if it is true (using an if statement or a ternary operator like below), then conditionally update the value. Also, you should update this value whenever the box is selected/unselected:

$(document).ready(function(){
    $('#edit-checkbox-id').change(function() {
        $("#gift-true").val( this.checked ? "Yes" : "" );
    }
});​

Also, if your checkbox is:

<input type="checkbox" name="gift" id="gift" class="checkbox" value="" />

The appropriate id-selector is $('#gift'), not $('#edit-checkbox-id')

Comments

0

$('#edit-checkbox-id').is(':checked') returns a boolean value true/false based on checkbox state. So you should use it as a condition and decide what to set the the value using val jQuery method. Try this.

$("#gift-true").val( $('#edit-checkbox-id').is(':checked') ? "Yes": "No" ); 

Comments

0

you can use CSS Pseudo-classe for example :

var checked=$('input[id="#gift-true"]:checked').length;
var isTrue=false;
if(checked)
    isTrue=true;
else
    isTrue=false;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.