Jump to content

adding events


garry27

Recommended Posts

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

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

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

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

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.