views:

641

answers:

2

I am trying to use jquery's parents/siblings to find particular input elements but I cannot seem to get this right.

I have the following HTML:

<div id="ExtrasOptions">
 <div class="selectItem">
    <div class="selectPrice"><span>Qty: <input name="qty" type="text" value="0" maxlength="2" id="qty" class="AccessoryQuantity" /></span></div>
    <div class="selectIt"><span><input name="extraselected" type="checkbox" id="extraselected" value="9" /><label for="extrasID">Add this</label></span></div>
 </div>
 <div class="selectItem">
    <div class="selectPrice"><span>Qty: <input name="qty2" type="text" value="0" maxlength="2" id="qty2" class="AccessoryQuantity" /></span></div>
    <div class="selectIt"><span><input name="extraselected2" type="checkbox" id="extraselected2" value="9" /><label for="extrasID">Add this</label></span></div>
 </div>
</div>

Q1: When someone checked a checkbox I want the textbox within the same div.selectItem to have a '1' put into it. If they uncheck the checkbox I want the value to be removed.

Q2: I also want the checkbox to be checked if a value is entered into the textbox and unchecked if the textbox is blank.

Thanks for your help.

+3  A: 

Something like this should work. (Not tested for precise syntax, but the algorithm is solid.)

// Bind an event to each of your checkboxes, for when they are clicked
$("input[type=checkbox]").click(function() {
  if ($(this).attr("checked")) {
    // The checkbox is checked, so find the associated text input and change its value
    $(this).parents(".selectItem").find("input[type=text]").val("1");
  } else {
    // The checkbox is unchecked, so find the associated text input and remove its value
    $(this).parents(".selectItem").find("input[type=text]").val("");
  }
});

// Bind an event to each of your text inputs, for when they have text entered into them
$("input[type=text]").keypress(function() {
  if ($(this).val() != "")) {
    // There is something in the text input box, so check the associated checkbox
    $(this).parents(".selectItem").find("input[type=checkbox]").attr("checked","checked");
  } else {
    // There is nothing in the text input box, so uncheck the associated checkbox
    $(this).parents(".selectItem").find("input[type=checkbox]").attr("checked","");
  }
});
bigmattyh
This does not work at all, $(this).parent() refers to a span, you need to move up two levels and get the div's next sibling.
karim79
As I said, the algorithm is solid, and perfectly a perfectly appropriate answer for this question.
bigmattyh
I think I'm missing something here. If I try your code for the checkboxes nothing happens. If use $(this).parents() I can get the value to go into the textbox but, obviously, it enters the value into all the textboxes. Any ideas how to have the value enter into the right textbox? thanks for your help!
Richard Reddy
Try the updated syntax, using parents(".selectItem") instead of parent().
bigmattyh
That's done it for me. Cheers for your help. You've really helped me out of a jam here! I adjusted your code slightly from .keypress to .blur on the second function as the value seemed to be what was previously entered in the textbox instead of what was just enterd...if that makes sense.
Richard Reddy
No problem. .keyup might also work for you, depending on what effect you're going for.
bigmattyh
+1  A: 

This works with your markup (tested):

$(document).ready(function() {
    $('input:checkbox').change(function() {
        if($(this).is(':checked')) {
            $(this).val('1');
        } else {
            $(this).val('');
        }
    });

    $('input[type=text]').keyup(function() {
        if($(this).val() != "") {
            $(this).parent()
                   .parent()
                   .next('.selectIt')
                   .find('span > input:checkbox')
                   .attr('checked','checked')
                   .val('1');
        } else {
            $(this).parent()
                   .parent()
                   .next('.selectIt')
                   .find('span > input:checkbox')
                   .removeAttr('checked')
                   .val('');
        }
    });
});
karim79