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?

1
  • 1
    Why don't you pass cde to the function? Commented Aug 18, 2009 at 5:51

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.