PDA

View Full Version : putting variable in event function



Ultragames
Mar 30th, 2007, 02:12 AM
I have this function, (the blah is obviously other code in the function)


function addAnotherType( obj, print_or_cart ){
// Blah , blah, blah

var dynamicSelect = document.createElement('SELECT');
dynamicSelect.name = 'types';
dynamicSelect.style.width = '99%';
dynamicSelect.onchange = function(){ addAnotherType(this); }

// Blah , blah, blah
}

Where you see the addAnotherType(this), I need to add another variable so that it becomes addAnotherType(this, THE_VALUE_OF_VARIABLE(print_or_cart) )

If i try just putting print_or_cart (a variable passed to the function) in my function call, it just puts the words print_or_cart there. How can I put the value of that function there, surrounded by single quotes?

Thank you.

shyam
Mar 30th, 2007, 08:14 PM
should actually do the trick


function addAnotherType( obj, print_or_cart ){
...
dynamicSelect.onchange = function(){
var porc = print_or_cart;
addAnotherType(this, porc);
};
...
}

Ultragames
Mar 30th, 2007, 08:45 PM
Is that just setting a new global variable that the function will reference? Because this function may be run many times before the onChange put on that element may be used. So the global variable might change.

If this actualy stores the value of the variable then this will work great. Anyone?

david_kw
Mar 30th, 2007, 08:58 PM
You shouldn't need another variable from what I see. The variable print_or_cart is not a global variable but rather a parameter to the function addAnotherType(). The anonymous function that is the onchange handler will save the value of print_or_cart (through closure) at the time of the call.



function addAnotherType( obj, print_or_cart ){
...
dynamicSelect.onchange = function(){
addAnotherType(this, print_or_cart);
};
...
}


Actually, thinking about it that will only be true if print_or_cart is an actual value and not a reference to something else. Is the value like a string or a number? Or is a reference to an object?

david_kw

Ultragames
Mar 30th, 2007, 10:03 PM
Its a string.

I'm calling addAnotherType(this, 'printer') or addAnotherType(this, 'cartridge')
I could just copy the onChange from the obj variable, and clone it to the new element, but I want to know how to get around this problem for times when I need it in the future.

I was worried about doing it like this:

dynamicSelect.onchange = function(){
addAnotherType(this, print_or_cart);
};

Because when I alert( dynamicSelect.onchange ); after setting it, it says print_or_cart, instead of the value of print_or_cart.

THanks for your help guys, keep it coming.

Ultragames
Mar 31st, 2007, 04:02 AM
Still need help on this one.

I am finding that when creating a function for the event, it puts in references to the variable, instead of its value. Similar to PHP's function( &$variable );

Here is the specific event that I am working with now:


cartridgeRow.onclick = function(){
youSure = confirm('Are you sure you want to add this cartridge to this printer?');
if( youSure == true ){
addCartridge( cartridge.getAttribute('id'), manufacturer.getAttribute('name'), cartridge.getAttribute('name'), cartridge.getAttribute('price_retail'), cartridge.getAttribute('price_user') );
}
}

This is inside of a loop. Every time the loop runs a new object gets created with this event, and those variables change value. Unfortunatly no matter when the event is called later, it uses the value that those variables had in the last itteration of the loop. Thus my thinking that this is a refference.

All of the tutorials I can find on events just has functions like doSomething() and don't show how to really pass it information.


Edit:
I was able to get it to work by putting the whole function in a string with the values concatenated in, and then setting the onclick to an eval() of that string. But this dosnt work in IE. There has to be a way to do this...!

david_kw
Mar 31st, 2007, 05:23 AM
I really should see the loop to comment, but here goes me guessing with one way to do it



var id = [], mname = [], cname = [], rprice = [], uprice = [];
for (int i = 0; i < 5; i++) {
cartridge = cartridges[i]; // total guess
manufacturer = manufacturers[i]; // total guess
id[i] = cartridge.getAttribute('id');
mname[i] = manufacturer.getAttribute('name');
cname[i] = cartridge.getAttribute('name');
rprice[i] cartridge.getAttribute('price_retail');
uprices[i] = cartridge.getAttribute('price_user');
cartridgeRow.localIndex = i; // assign to a custom property for this row
cartridgeRow.onclick = function(){
youSure = confirm('Are you sure you want to add this cartridge to this printer?');
if( youSure == true ){
var index = this.localIndex;
addCartridge( id[index], mname[index], cname[index], rprice[index], uprice[index] );
}
}
}


Maybe something like that would work? Totally untested of course but I think it is right in theory.

david_kw