Jin Yong
Jin Yong

Reputation: 43778

Any way that I can replace the function variable with javascript?

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?

Upvotes: 0

Views: 98

Answers (3)

Pete  Jordan
Pete Jordan

Reputation: 548

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.

Upvotes: 1

Blixt
Blixt

Reputation: 50169

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'); };

Upvotes: 0

Eldar Djafarov
Eldar Djafarov

Reputation: 24677

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.

Upvotes: 0

Related Questions