void Posted January 7, 2010 Share Posted January 7, 2010 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? Quote Link to comment Share on other sites More sharing options...
Adam Posted January 7, 2010 Share Posted January 7, 2010 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.. Quote Link to comment Share on other sites More sharing options...
Irresistable Posted January 7, 2010 Share Posted January 7, 2010 Are you trying to create the function with the value of "text" ? Quote Link to comment Share on other sites More sharing options...
void Posted January 7, 2010 Author Share Posted January 7, 2010 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 :-) Quote Link to comment Share on other sites More sharing options...
Irresistable Posted January 7, 2010 Share Posted January 7, 2010 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. Quote Link to comment Share on other sites More sharing options...
void Posted January 7, 2010 Author Share Posted January 7, 2010 <...>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. Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted January 7, 2010 Share Posted January 7, 2010 <...>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? Quote Link to comment Share on other sites More sharing options...
Adam Posted January 7, 2010 Share Posted January 7, 2010 This should solve your problem.. http://episteme.arstechnica.com/eve/forums/a/tpc/f/6330927813/m/250006870931 Quote Link to comment Share on other sites More sharing options...
void Posted January 7, 2010 Author Share Posted January 7, 2010 This should solve your problem.. http://episteme.arstechnica.com/eve/forums/a/tpc/f/6330927813/m/250006870931 It did! Absolutely what i was looking for. Thanks Adam! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.