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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.