List Info

Thread: Binding in jQuery




Binding in jQuery
user name
2008-06-16 13:03:26
Ok, so I know that the Prototype `bind` method isn't the
correct way
to go about things in jQuery. The problem is I don't know
what the
correct way is…So, I offer up an example:

$(document).ready(function(){
	$('h2.countdown[paused="no"]').each(function(){
		this.update = function()
		{
			this.innerHTML = 'stuff';
		}
		this.interval = setInterval(this.update, 1000);
	});
});

The idea here is that every second the update function runs
on the
selected h2's. Meaning every second a function call should
be
triggered with the h2 as the scope. Is this not possible or
even the
correct way to do this? How should I do this in jQuery?

Re: Binding in jQuery
user name
2008-06-16 15:58:10
There's nothing wrong with using a bind() function - it's
just that jQuery
doesn't provide one. It isn't really necessary, because in
any situation
where you might use bind(), you can use a closure - and the
code is usually
simpler. In fact, a closure is what bind() uses.

Here is one way you could code it:

$(function(){
	$('h2.countdown[paused="no"]').each(function( i,
element ){
		setInterval( function() {
			element.innerHTML = 'stuff';
			  // or do it the jQuery way:
			$(element).html( 'stuff' );
		}, 1000 );
	});
});

I didn't see the code using this.inverval - if you have an
example of how
that might be used, I can give you another idea or two on
it.

-Mike

> Ok, so I know that the Prototype `bind` method isn't
the 
> correct way to go about things in jQuery. The problem
is I 
> don't know what the correct way is.So, I offer up an
example:
> 
> $(document).ready(function(){
>
	$('h2.countdown[paused="no"]').each(function(){
> 		this.update = function()
> 		{
> 			this.innerHTML = 'stuff';
> 		}
> 		this.interval = setInterval(this.update, 1000);
> 	});
> });
> 
> The idea here is that every second the update function
runs 
> on the selected h2's. Meaning every second a function
call 
> should be triggered with the h2 as the scope. Is this
not 
> possible or even the correct way to do this? How should
I do 
> this in jQuery?
> 


[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )