Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript modify parameters of an element's event function
    primarykey
    data
    text
    <p>Im wondering if there is a more elegant means of modifying the parameter of an onclick event. I have a table that I am dynamically adding/removing elements from and I reindex the rows. Each row has a <code>delete</code> link that has the row's index (and a <code>duplicate</code> link) that needs to update its parameter to match the modified row id. </p> <p>Currently my code looks like (simplified)</p> <pre><code>&lt;a onclick="delRow(1)"&gt;delete&lt;/a&gt; </code></pre> <p>and the javascript: ...</p> <pre><code>html = element.innerHTML; html = html.replace(/dupRow(\\d+)/g, "dupRow(" + newIndex + ")"); html = html.replace(/delRow(\\d+)/g, "delRow(" + newIndex + ")"); element.innerHTML = html </code></pre> <p>and I would like it to become something along the lines of</p> <pre><code>if (element.onclick != null) { element.onclick.params[0] = newIndex; } </code></pre> <p>Any such way of accomplishing this? I also have jQuery if this helps. Thanks!</p> <p><strong>Updates:</strong></p> <p>So thanks to the glorious help of @rich.okelly I have solved my issue</p> <pre><code>&lt;script&gt; ... var newRow = '\ &lt;tr&gt;\ &lt;td class="index" col="0"&gt;0&lt;/td&gt;\ &lt;td&gt;this is content...&lt;/td&gt;\ &lt;td&gt;&lt;a href="#" row-delete="true"&gt;Del&lt;/a&gt;&lt;/td&gt;\ &lt;/tr&gt;'; // re-index table indices in a non-efficient manner function reIndexTable() { $("#rpc-builder-table").find('.index').each(function (i) { $(this).html(i) }) } // add row function addRow() { for (i = 0; i &lt; $('#addRowCount').attr("value"); i++) { $("#rpc-builder-table").append(newRow); } reIndexTable(); } $(document).ready(function () { // add row button $('#addRowsButton').on('click', function () { addRow(); }); // delete row $('#rpc-builder-table').on('click', 'td a[row-delete="true"]', function () { $(this).closest('tr').remove(); reIndexTable(); }); ... } &lt;/script&gt; ... &lt;div&gt; &lt;label&gt;Rows to add: &lt;/label&gt; &lt;input id="addRowCount" value="1" size="2" /&gt; &lt;button id="addRowsButton"&gt;Add Row(s)&lt;/button&gt; &lt;/div&gt; &lt;div&gt;&lt;table id="rpc-builder-table"&gt;&lt;tbody&gt; &lt;tr&gt; &lt;th&gt;Idx &lt;/th&gt; &lt;th&gt;Some content (1)&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt; ... </code></pre> <p>I used the <code>.on()</code> function instead of the suggested <code>.delegate()</code> function since it is deprecated. Solution works well - hope it helps someone :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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