0

Does anyone know how can I replace the passing variable in html with Javascript?

Example:

If I have a code as below:

<table width="150" border="0" cellspacing="0" cellpadding="2" id="productBox">
<tr>
    <td valign="top" height="19" id="td4"><img onclick="addNewRowToTable('abc')" src="images/add0.gif" onmouseover="this.src=\'images/add1.gif\'" onmouseout="this.src=\'images/add0.gif\'" class="handCursor" width="49" height="19"></td>
</tr>
</table>

Any way that I can replace variable 'abc' to 'cde' with javascript?

Jin Yong
  • 42,698
  • 72
  • 141
  • 187

3 Answers3

1

You can (as noted by others), but I suspect that you might get a more useful answer if we knew what you were actually trying to do; there's pretty much certainly a better way of approaching your problem, but I'd need context to suggest what it might be.

Pete Jordan
  • 558
  • 4
  • 2
0

You can pass a global variable to function. And define this variable higher in code.

<script>
var globalVar='cdb';
</script>

<table width="150" border="0" cellspacing="0" cellpadding="2" id="productBox">
<tr>
    <td valign="top" height="19" id="td4"><img onclick="addNewRowToTable(globalVar)" src="images/add0.gif" onmouseover="this.src=\'images/add1.gif\'" onmouseout="this.src=\'images/add0.gif\'" class="handCursor" width="49" height="19"></td>
</tr>
</table>

if you will change globalVar later it will affect your code.

Eldar Djafarov
  • 23,327
  • 2
  • 33
  • 27
0

If you want to replace the onclick attribute to call addNewRowToTable('cde') instead of what it's calling now, I would suggest rebinding the event:

var img = // Here you would get the <img> somehow
img.onclick = function () { addNewRowToTable('cde'); };
Blixt
  • 49,547
  • 13
  • 120
  • 153