Jump to content

assiging functions to events & passing variables


void

Recommended Posts

Hi,

 

I came across something which would look pretty straightforward, but apparently is not. Or that's just me and 2am on the clock. Anyways, the code:

 

var text = 'blahblahblah';
document.getElementById('whatever').onmouseover = function() { myfunction(text); }

 

the problem is that i want to parse the variable 'text' right here. that is, i need 'whatever' object to bind function myfunction('blahblahblah'), not myfunction(text). how do i do that? :wtf:

Without being able to see the context the code is being used in, I'm guessing you're trying to do this before the DOM has loaded?

 

Give this a try:

 

window.onload = function() {
    var text = 'blahblahblah';
    document.getElementById('whatever').onmouseover = function() { myfunction(text); }
}

 

Though I'm not fully sure I understand the question..

Are you trying to create the function with the value of "text" ?

 

Yes. Okay, to give you more understanding of what I'm doing, here's the code:

 

// I have an array with field id as a key and simple text as a value
var ar = new Array();
ar['field1'] = "text for field1";
ar['field2'] = "text for field2";
ar['field3'] = "text for field3";

// now i have a loop which assigns the text to be alerted for each of the fields in array, when you click on it.

for(var i in ar) {
document.getElementById(i).onclick = function() { alert(ar[i]); }
}

 

and so this is where it all screws up. it doesn't assign the function alert("text for field1") for field1, instead it assigns alert(ar[ i ]), which is not what i want.

maybe what i'm doing doesn't make much sense and someone can tell me a better way to achieve what i want. insults and mockery are welcome :-)

Whats the element "i"

What happens with what you have now?

What is suppose to happen?

 

I don't think you've explained well enough. What it looks like, you have a field, div whatever, and it's got an ID of "i" When you click it, it gives an alert. That's all I'm understanding right now.

<...>When you click it, it gives an alert. That's all I'm understanding right now.

 

That's all you need to understand. The problem is with the text in the alert box - it does not correspond to the field in the array. It will always alert the last message in the array, because each element's onclick action is binded with alert(ar[ i ]), which is variable, and not alert("text for field*id*"), which is a simple string and which is what i want.

<...>When you click it, it gives an alert. That's all I'm understanding right now.

 

That's all you need to understand. The problem is with the text in the alert box - it does not correspond to the field in the array. It will always alert the last message in the array, because each element's onclick action is binded with alert(ar[ i ]), which is variable, and not alert("text for field*id*"), which is a simple string and which is what i want.

 

So what's actually being displayed in the alert?  ar['field3']?  And is this the case for all the elements?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.