Links

Lists

Latest Updates

Ruby On Rails List
Python list
Advanced Java
The JavaScript List
Apache Users
Full Disclosure
Linux Security

Search the archives!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Javascript] Problem w/Javascript, interpolation, functions, onclick


  • From: nvp at noopy.org (Nathan V. Patwardhan)
  • Subject: [Javascript] Problem w/Javascript, interpolation, functions, onclick
  • Date: Wed Oct 25 10:25:13 2006

On Tue, Oct 24, 2006 at 11:52:36PM -0400, Matt Warden wrote:
: 
: I could get your eval to work for you, but then I'd have to take 30
: showers. And I still wouldn't feel clean.

Sorry.  :-(

: var actions = new Array (
:   ['Cancel', function() {edit_release(this, "cancel")}],
:   ['Submit', function() {submit_release(this)}]
: );
: 
: Now, lord knows what you're trying to do with passing 'this'. If you
: tell us what you want 'this' to refer to, we can probably help you out
: there. Whatever you're wanting it to point to, it ain't.

I'm modifying table cells based on an onclick event.  So, for a table
row that contains:

  <td><input type="text" name="release" value="somevalue"/></td>
  <td><input type="text" name="version" value="someother"/></td>
  <td>
    <span id="addthis" onclick="function1(this)">Add This</span>
    <span id="cancel" onclick="function2(this)">Cancel</span>
  </td>

I pass 'this' to a function -- since 'this' represents the current span
that was selected.  My code finds the last (third) cell in the current
row (of 'this'), and modifies contents of the same table row to reflect
which of the spans was selected.

Since I'm populating table cells dynamically, this includes setting
attributes for a new node that will occupy a specific table cell; this
includes setting an onclick attribute for the new node.  As I am adding
several spans to a single table cell, I created/replaced each node in
a for-loop:

  var actions = new Array( ... etc ... );

  var my_cell = this.parentNode;     // current table cell
  var old_node = my_cell.firstChild; // contents of current table cell
  for(var i=0; i<action_count; i++) {
      var new_node = document.createElement('span');

      // assign contents of old node to new node
      new_node.id = old_node.name;
      new_node.innerHTML = actions[i][0];
      new_node.onclick = actions[i][1];
      my_cell.replaceChild(new_node, old_node);
  }

-- 
Nathan V. Patwardhan
nvp@xxxxxxxxx
aim: notoriousnvp71
"Electric guitars in church?! Some dropouts must've overpowered the
 reverend!" -- Ned Flanders