garry27 Posted February 5, 2007 Share Posted February 5, 2007 hi i'm trying to dynamically add an on click event to a row. it needs to pass a 'this' variable to a function to specify the node that sent it. what's the best way to go about this? Link to comment https://forums.phpfreaks.com/topic/37158-adding-events/ Share on other sites More sharing options...
fenway Posted February 5, 2007 Share Posted February 5, 2007 I'm not sure what you mean... how would it be any different than a non-dynamic one? Link to comment https://forums.phpfreaks.com/topic/37158-adding-events/#findComment-177481 Share on other sites More sharing options...
garry27 Posted February 5, 2007 Author Share Posted February 5, 2007 i have table which i add rows to in js and i want each row to contain an event that links it to a function. i'm trying to use the onclick event but it won't pass a parameter. i get 'missing formal parameter' in firebug. here's the code i have: ... var row = document.createElement("tr"); row = onClick = thisRow(this); ... function thisRow(x){ whichrow.style.backgroundColor='transparent'; whichrow = x; whichrow.style.backgroundColor='yellow'; } Link to comment https://forums.phpfreaks.com/topic/37158-adding-events/#findComment-177490 Share on other sites More sharing options...
garry27 Posted February 5, 2007 Author Share Posted February 5, 2007 sorry,the previous function should have read: function thisrow(x){ if (whichrow) whichrow.style.backgroundColor='transparent' whichrow = x whichrow.style.backgroundColor='yellow' } Link to comment https://forums.phpfreaks.com/topic/37158-adding-events/#findComment-177491 Share on other sites More sharing options...
fenway Posted February 5, 2007 Share Posted February 5, 2007 Don't you mean row.onclick = "thisrow(this)"? Link to comment https://forums.phpfreaks.com/topic/37158-adding-events/#findComment-177493 Share on other sites More sharing options...
garry27 Posted February 5, 2007 Author Share Posted February 5, 2007 ye, that's what i meant Link to comment https://forums.phpfreaks.com/topic/37158-adding-events/#findComment-177502 Share on other sites More sharing options...
fenway Posted February 5, 2007 Share Posted February 5, 2007 So it still doesn't work? Link to comment https://forums.phpfreaks.com/topic/37158-adding-events/#findComment-177532 Share on other sites More sharing options...
garry27 Posted February 5, 2007 Author Share Posted February 5, 2007 its not sending the row reference. the alert function in thisRow() says [object window] Link to comment https://forums.phpfreaks.com/topic/37158-adding-events/#findComment-177553 Share on other sites More sharing options...
fenway Posted February 5, 2007 Share Posted February 5, 2007 I didn't know you could get a "row" reference.... Link to comment https://forums.phpfreaks.com/topic/37158-adding-events/#findComment-177742 Share on other sites More sharing options...
garry27 Posted February 7, 2007 Author Share Posted February 7, 2007 i'm using innerhtml now to add events to row elements. it takes longer this way because you have to rebuild the table each time you add or remove any rows but it does the job. with the following tr tag, the onclick event passes a reference of the position of that row to the thisRow function. <tr onclick='thisRow(this)'><td> then you can do whaterver you like with it. i.e: function thisRow(x){ x.className='selrow'; } i hope i've helped. thanks by the way for figuring out that awkward link i was trying to ouput in js. Link to comment https://forums.phpfreaks.com/topic/37158-adding-events/#findComment-179015 Share on other sites More sharing options...
Zeon Posted February 7, 2007 Share Posted February 7, 2007 it's better without innerHTML, you just used it wrong. ... var row = document.createElement("tr"); row.onclick = function(){thisRow(this)}; // <--- ... function thisRow(x){ whichrow.style.backgroundColor='transparent'; whichrow = x; whichrow.style.backgroundColor='yellow'; } it should work just fine. Cheers. Link to comment https://forums.phpfreaks.com/topic/37158-adding-events/#findComment-179020 Share on other sites More sharing options...
fenway Posted February 7, 2007 Share Posted February 7, 2007 Right... function... I knew it looked funny. Link to comment https://forums.phpfreaks.com/topic/37158-adding-events/#findComment-179198 Share on other sites More sharing options...
ozfred Posted February 14, 2007 Share Posted February 14, 2007 Right... function... I knew it looked funny. If all you want is a reference back to the element that fired the event, you don't need to pass any paramters: ... thisrow.onclick = foo; ... function foo(){ var thisrow = this; } foo's this keyword will be set as a reference to the element that called it. -- Fred Javascript FAQ: <URL: http://www.jibbering.com/faq/ > Link to comment https://forums.phpfreaks.com/topic/37158-adding-events/#findComment-184875 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.