Note that there are some explanatory texts on larger screens.

plurals
  1. POjavascript scope question
    text
    copied!<p>I've written a little Ajax-y page, that displays data rows from an ajax request. Each row has a "delete link" to allow the user to delete the row quickly, also via ajax.</p> <p>My problem is one with the scope of the function--</p> <p>The "main" page has a function that looks like this:</p> <pre><code>&lt;script language="javascript" type="text/javascript"&gt; window.addEvent('domready', function() { function getRows(){ var myurl = 'myajaxurl.php'; var req = new Request({ async:false, method: 'get', url: myurl, data: {'id':'&lt;?php echo $id; ?&gt;'}, evalScripts:true, onSuccess: function(response) { $('my_rows_div').set('html',response); } }).send(); } function deleteRow(rowid){ alert(rowid); } }); </code></pre> <p></p> <p>The data being returned by the 'getRows' function is just some html rows, and it looks like this:</p> <pre><code>&lt;tr&gt;&lt;td&gt;data&lt;/td&gt;&lt;td&gt;&lt;a onclick="deleteRow(1)" href="javascript:void(0);"&gt;delete&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;data&lt;/td&gt;&lt;td&gt;&lt;a onclick="deleteRow(2)" href="javascript:void(0);"&gt;delete&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt; </code></pre> <p>So far so good-- but when I render the page and click the "delete" link, I get the js error that "deleteRow is not defined".</p> <p>I know this probably has something to do with scope, but I don't quite understand how this works. How can I reference the "parent" page's deleteRow function from within the returned ajax data?</p> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload