views:

82

answers:

6

Why is line 10 returning null?

http://pastie.org/720484

it works with line 40

+1  A: 

try fetching the input:text's .val() instead

Scott Evernden
A good idea, yes, but doesn't have anything to do with why the siblings() function doesn't find anything.
tvanfosson
+2  A: 

You do not seem to have a proper grasp of the siblings() operator. You also were not utilizing jQuery's val() function and were missing periods on some of your class names. To locate the address1 class you would need to do the following:

var $checkbox = jQuery(this); 
$checkbox.parent().siblings('.formField').find('.address1');

Also, you would want the alert to be

alert($checkbox.parent().siblings('.formField').find('.address1').val());

to alert the value of the input box.

FIXED AND OPTIMIZED VERSION:

function update_address(eventObject) { 
  var $checkbox = jQuery(this);
  var $siblings = $checkbox.parent().siblings('.formField');
  if ($checkbox.attr('checked')) { 
      $siblings.find('.address1').val($('.hidden_address1').val());
      $siblings.find('.address2').val($('.hidden_address2').val());
      $siblings.find('.city').val($('.hidden_city').val());
      $siblings.find('.state').val($('.hidden_state').val());
      $siblings.find('.zip').val($('.hidden_zip').val());
      $siblings.find('.province').val($('.hidden_province').val());
      $siblings.find('.country').val($('.hidden_country').val());
  } else { 
      $siblings.find('.address1').val('');
      $siblings.find('.address2').val('');
      $siblings.find('.city').val('');
      $siblings.find('.state').val('');
      $siblings.find('.zip').val('');
      $siblings.find('.province').val('');
      $siblings.find('.country').val('');
  }    
}
cballou
A: 

Because <input class="address1"/> is not a sibling of <input id="parent_sameAsBefore"/>. I think you want:

checkbox.parent().parent().find('.address1');
J-P
+1  A: 

On line 9, shouldn't it be var checkbox = $(this); instead? I've not seen the jQuery() function used like that.

Nate B
urm... jQuery === $
J-P
Might be using jQuery in `noConflict` mode, more here: http://docs.jquery.com/Using_jQuery_with_Other_Libraries
Pablo
you do know that $ == jQuery ?
Scott Evernden
A: 

alert(checkbox.siblings('.address1').html() ); // This should be

alert(checkbox.parent().siblings('.address1').html() );

//Checkbox does not have siblings Line 10

Colour Blend
`<input/>` elements are supposed to be empty. In HTML, you don't have to explicitly close them, but in XHTML you need to "self-close" them. i.e. `<input/>`
J-P
@J-P: Thanks for the observation. He is trying to get the siblings of checkbox. He will get null cause checkbox has no siblings.
Colour Blend
A: 

Why not just go with finding the form fields using absolute path? Unless your DOM is very convoluted (and you need relative paths), I would prefer this approach myself.

Also use .val() to get and set values.

function update_address(eventObject) { 
  if($(this).attr('checked')) { 
    $('#parent_address1').val($('hidden_address1').val()); 
    $('#parent_address2').val($('hidden_address2').val()); 
    $('#parent_city').val($('hidden_city').val()); 
    $('#parent_state').val($('hidden_state').val()); 
    $('#parent_zip').val($('hidden_zip').val()); 
    $('#parent_province').val($('hidden_province').val()); 
    $('#parent_country').val($('hidden_country').val()); 
  }    
  else { 
    $('#parent_address1').val("");  
    $('#parent_address2').val("");  
    $('#parent_city').val("");  
    $('#parent_state').val("");  
    $('#parent_zip').val("");  
    $('#parent_province').val("");  
    $('#parent_country').val("");  
  }    
}

Note, seems to be a bug in the original code in line 15:

checkbox.siblings('.tate').value = $('hidden_state').value;

Should be:

checkbox.siblings('.state').value = $('hidden_state').value;
Sune Rievers
If noConflict mode is in effect, just replace $ with jQuery in above code
Sune Rievers
probablycheckbox.siblings('.state').val() = $('hidden_state').val();???
Mark Schultheiss
That would be better, but the error is the missing 's' in state :)
Sune Rievers
I don't think `o1.val() = o2.val()` will change o1. Afaik, and looking at the JQuery website, the new value should be a parameter, like so: `checkbox.siblings('.state').val($('hidden_state').val())`
Sune Rievers