coderb Posted January 14, 2008 Share Posted January 14, 2008 Hi All, I must be doing something wrong. I have a from with an input field and button. On change of input field I want to disable the button. this works: <input name="subject" type="text" onchange="mailform.Print.disable = true;"/> if I now change this to be executed in a function, it no longer works: <input name="subject" type="text" onchange="Print_disable()"/> <script language="javascript"> function Print_disable () { alert("hello"); mailform.Print.disable = true; } </script> to confirm that this funciton is executed I added the alert. But my Print button is not disabled. Any help appreciated, thanks. Quote Link to comment Share on other sites More sharing options...
dooper3 Posted January 14, 2008 Share Posted January 14, 2008 So the alert shows up but the Print button doesn't disable, or both don't happen? Start by putting the function script above the form input, not below. Quote Link to comment Share on other sites More sharing options...
coderb Posted January 14, 2008 Author Share Posted January 14, 2008 I just pasted this example in that order. I have the javascript funtion in the <head> section of my page. Quote Link to comment Share on other sites More sharing options...
coderb Posted January 14, 2008 Author Share Posted January 14, 2008 ..and the alert shows up the disable does not happen. Quote Link to comment Share on other sites More sharing options...
dooper3 Posted January 14, 2008 Share Posted January 14, 2008 Try putting the alert after the disabling part of the function, as an alert may stop the script running. Quote Link to comment Share on other sites More sharing options...
coderb Posted January 14, 2008 Author Share Posted January 14, 2008 tried that, but now the alert doesn't work either. Quote Link to comment Share on other sites More sharing options...
dooper3 Posted January 14, 2008 Share Posted January 14, 2008 I'm no javascript expert, but try changing the name of the function to something else. Some languages don't allow you to start variables and functions with words like "Print". Change it to something random like banana to test this. edit: Oops... if it was showing the alert before then it was not the name of the function causing the problem, ignore me! Quote Link to comment Share on other sites More sharing options...
coderb Posted January 14, 2008 Author Share Posted January 14, 2008 thanks for trying. anyone else got any ideas? could it be a browser issue, could someone maybe test my code: this works: <form name="mailform" ... <input name="subject" type="text" onchange="mailform.Printx.disabled = true;"/> <input name="Printx" type="submit" value="Print Friendly"> this doesn't: <head> <script type="text/javascript"> function Printx_disable () { mailform.Printx.disabled = true; } </script> <form name="mailform" ... <input name="subject" type="text" onchange="Printx_disable ()"/> <input name="Printx" type="submit" value="Print Friendly"> Quote Link to comment Share on other sites More sharing options...
emehrkay Posted January 14, 2008 Share Posted January 14, 2008 try changing this line mailform.Printx.disabled = true; to document.forms['mailform']['Printx'].disabled = true; Quote Link to comment Share on other sites More sharing options...
coderb Posted January 14, 2008 Author Share Posted January 14, 2008 thank you, thank you, thank you. that worked. I previously tried the getelementbyId, but that didn't work. is there a link that explains why this has to change? Quote Link to comment Share on other sites More sharing options...
emehrkay Posted January 14, 2008 Share Posted January 14, 2008 I would prefer looking the element up by id like document.getElementById('PrintX').disabled = true; //if Printx is the id of the element As to why what I wrote works, it is because the document.forms array holds all of the forms. you can assess your mailform by its name or index. then the form has an array of the elements that you can assess either way as well Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.