[jQuery] Re: Finding previous next input siblings encapsulated in other li ?

2009-10-16 Thread Julien

Thanks a lot, Karl. Your code dit it.

Now are several events (like key strokes and checkbox changes) for
which the same code must be run.
So, I would like to retrieve the siblings from within a function.

The problem I encounter with the code below is how to retrieve in
jQuery the caller argument passed to the function ?  Is there some
general syntax to read variables ?

 $(':text')
 .focus( function { UpdateFooter(this) } )
 .change( function { UpdateFooter(this) } )

$(':checkbox')
 .change( function { UpdateFooter(this) } );

function UpdateFooter (caller) {
$(caller).parent().nextAll().find('input:text').each(doSomething
());   // Which syntax to read the parameter ?
}


On 15 oct, 19:31, Karl Swedberg k...@englishrules.com wrote:
 Provided that all the text inputs located after it are within the same  
 ul, this should do it:

 $('input:text').change(function() {
    $(this).parent().nextAll().find('input:text').doSomething();

 });
(skipped)


[jQuery] Re: Finding previous next input siblings encapsulated in other li ?

2009-10-16 Thread Karl Swedberg

Hi Julien,

May I propose a different way?

$('input:checkbox, input:text').bind('change focus', function(event) {
  if (event.type == 'change' || this.type == 'text') {
$(this).parent().nextAll().find('input:text').doSomething();
  }
});

This selects all checkboxes and text inputs and binds both a change  
and a focus handler to them. The doSomething() method will fire on  
change regardless of the input type (event.type == 'change') and on  
focus for text inputs only (this.type == 'text')


--Karl


Karl Swedberg
www.englishrules.com
www.learningjquery.com




On Oct 16, 2009, at 6:02 AM, Julien wrote:



Thanks a lot, Karl. Your code dit it.

Now are several events (like key strokes and checkbox changes) for
which the same code must be run.
So, I would like to retrieve the siblings from within a function.

The problem I encounter with the code below is how to retrieve in
jQuery the caller argument passed to the function ?  Is there some
general syntax to read variables ?

$(':text')
.focus( function { UpdateFooter(this) } )
.change( function { UpdateFooter(this) } )

$(':checkbox')
.change( function { UpdateFooter(this) } );

function UpdateFooter (caller) {
   $(caller).parent().nextAll().find('input:text').each(doSomething
());   // Which syntax to read the parameter ?
}


On 15 oct, 19:31, Karl Swedberg k...@englishrules.com wrote:
Provided that all the text inputs located after it are within the  
same

ul, this should do it:

$('input:text').change(function() {
   $(this).parent().nextAll().find('input:text').doSomething();

});

(skipped)




[jQuery] Re: Finding previous next input siblings encapsulated in other li ?

2009-10-16 Thread Julien

Hi Karl,

Thanks for your interesting alternative approach.

On my side I was trying to set a jQuery variable using the argument
that was passed to the function.
But something seems broken if we don't use jQuery from the very
beginning for the selection.

$.tmp = caller;
alert($.tmp);// displays object HTMLInputElement
alert($.tmp.parent());  // Does not work.
alert($.tmp.parentElement);   //  displays object HTMLLIElement, but
impossible to go further with find(), a.s.o.


[jQuery] Re: Finding previous next input siblings encapsulated in other li ?

2009-10-16 Thread Julien

Karl,
I choose to keep my original approach as it was better for code
clarity in my case.

From within my function, I could select the text input passed as
argument with variable $currentField.
I could also get the list of other text inputs located after it.
$afterFields.length correctly counts the number of those fields.

Now, for each of those text fields I must access the status of the
checkbox located just after.

Instead of .each(), I would like to use a for loop since the loop
must be exited as soon as a checked checkbox is found.

I've been unable to access the checkbox.

function UpdateFooter(caller) {
(skip)

var $currentField = :text[name= + caller.name + ];
var $afterFields = $($currentField).parent().nextAll().find(:text);

// Use a for loop instead of a .each() handler.
var n = $afterFields.length;
if ( n  0) {  // If there are text fields located after the current
text field.
for (var i=0; in; i++) {
alert($('$afterFields[i]~:checkbox').checked);  // Does not
work.
}
}

(skip)
}  // end of function


[jQuery] Re: Finding previous next input siblings encapsulated in other li ?

2009-10-15 Thread Julien

Hi again,

My question was probably too long as I got no answer...

When a change occurs is one of the following text fields, does someone
know how to scan through all the text inputs located after it?

fieldset name=myfields
ul

  liinput type=text name=txt_one /
  input type=checkbox name=chk_one //li

  liinput type=text name=txt_two /
  input type=checkbox name=chk_two //li

  liinput type=text name=txt_three /
  input type=checkbox name=chk_three //li

/ul

...assuming that there are more fields and I need some jQuery
automation.

Thanks for your help.


[jQuery] Re: Finding previous next input siblings encapsulated in other li ?

2009-10-15 Thread Karl Swedberg


Provided that all the text inputs located after it are within the same  
ul, this should do it:


$('input:text').change(function() {
  $(this).parent().nextAll().find('input:text').doSomething();
});

If you need to check in multiple uls or fieldsets, we'll need to  
tweak the DOM traversal to match the others as well.


--Karl


Karl Swedberg
www.englishrules.com
www.learningjquery.com




On Oct 15, 2009, at 12:07 PM, Julien wrote:



Hi again,

My question was probably too long as I got no answer...

When a change occurs is one of the following text fields, does someone
know how to scan through all the text inputs located after it?

fieldset name=myfields
ul

 liinput type=text name=txt_one /
 input type=checkbox name=chk_one //li

 liinput type=text name=txt_two /
 input type=checkbox name=chk_two //li

 liinput type=text name=txt_three /
 input type=checkbox name=chk_three //li

/ul

...assuming that there are more fields and I need some jQuery
automation.

Thanks for your help.