Armed Rebel Posted March 27, 2007 Share Posted March 27, 2007 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 More sharing options...
aspnetguy Posted March 27, 2007 Share Posted March 27, 2007 try this instead c2.onchange = function(event) { replaceWord(inc1, (i - 1), r, (total - 1)) }; Link to comment Share on other sites More sharing options...
Armed Rebel Posted March 28, 2007 Author Share Posted March 28, 2007 Nope, the script still fails and the params are being passed as literals rather than the variable's value. Link to comment Share on other sites More sharing options...
adservio Posted March 28, 2007 Share Posted March 28, 2007 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 More sharing options...
Patrick Posted March 29, 2007 Share Posted March 29, 2007 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now