Why does my variable reference keep changing?

Why does my variable reference keep changing?

Hi,

I'm relatively new to JQuery and am having trouble with a particular feature of its semantics.

It looks if I assign a JQuery object to a variable like this...

var test_var = $('#my_button');


...then do something like this...

var test_var2 = test_var.next();


...I'm finding that both the variables now reference the same object i.e. the one that comes after 'my_button' in the DOM. As a programmer I would expect calling the next() method to return the next object but I wouldn't expect it to actually change the original variable reference. To me, it seems to undermine the whole purpose of a variable.

Can anyone a) explain why this is the case, and b) explain how I can get around this. For example, if I pass an object into a Javascript function then I'm likely to want to use that object throughout the duration of that function, but if I use it to traverse the DOM then suddenly I no longer know what object was passed into the function.

It may be that I'm failing to understand something fundamental here in which case I apologise, but I hope someone can clear this up for me. Thanks in advance.
    • Topic Participants

    • mail