Jump to content

Re-setting an onchange event


Armed Rebel

Recommended Posts

I'm trying to change an onchange event, and I've got the code to do it and it's (almost) working, but I'm having a little problem with variables. Using this code:

c2.onchange = function onchange(event) { replaceWord(inc1, (i - 1), r, (total - 1)) };

The variables int1, i, r and total have all been declared in the script I'm using with the above line, the problem is that instead of setting the new function with the values those variables contain, it sets the variable's names as a literal string so "inc1" become the parameter. The code (of actually setting the new onchange function) works, as if I tried alert('hi') it would work fine.Any solutions to this?

Link to comment
Share on other sites

You may need to post more of your source code.Here's something that might help you spot your problem.Tested in IE, O, FF.

  <script type="text/javascript">    var test = 'test';    var i = 5;    var j = 6	function showThis(s, int) {		alert(s + ' ' + int);	}	function chgOnChange(id) {		document.getElementById(id).onchange = function() { showThis(test, j - i); };	}  </script> </head> <body>	<input type="text" id="t1" value="test this" />	<script type="text/javascript">chgOnChange('t1');</script>

Link to comment
Share on other sites

I'm trying to change an onchange event, and I've got the code to do it and it's (almost) working, but I'm having a little problem with variables. Using this code:
c2.onchange = function onchange(event) { replaceWord(inc1, (i - 1), r, (total - 1)) };

Try this instead:
c2.onchange = new Function(replaceWord(inc1, (i - 1), r, (total - 1)))

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...