Darkmatter5 Posted August 5, 2009 Share Posted August 5, 2009 Here's my Javascript function disablefield(form,element) { if(element='att_pc') { document.form.att_npc.disabled=true; document.form.att_beast.disabled=true; } elseif(element='att_npc') { document.form.att_pc.disabled=true; document.form.att_beast.disabled=true; } elseif(element='att_beast') { document.form.att_pc.disabled=true; document.form.att_npc.disabled=true; } } Here's the HTML <form method='get' name='simcombat' action='http://mysite/simcombat.php?ta=20'> <table border='0' bgcolor='#D2B48C'> <tr><th colspan='4'>Combat simulator</th></tr> <tr><th> </th><th>Player characters</th><th>Nonplayer characters</th><th>Beasts</th></tr> <tr> <td align='right'>Attacker:</td> <td align='center'> <select name='att_pc' onchange='disablefield(this.form,this.name)'> <option value='0'>--SELECT--</option> <option value='13'>a b</option> <option value='2'>Joe Blow</option> </select> </td> <td align='center'> <select name='att_npc'> <option value='0'>--SELECT--</option> <option value='2'>Joe Bloe</option> <option value='1'>John Smith</option> </select> </td> <td align='center'> <select name='att_beast'> <option value='0'>--SELECT--</option> <option value='4'>Cat</option> <option value='3'>Rat</option> </select> </td> </tr> <tr><th colspan='4'><input type='submit' name='submit' value='Simulate combat'></th></tr> <table> </form> I'm basically wanting to disable all form elements besides the element changed. I know my usage of "this" is incorrect. Please help! Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 5, 2009 Share Posted August 5, 2009 1. Need to use double equal signs when doing comparisons, a==b> A single equal sign will assign a value. 2. You can't use "form" like that (unless you named your form as "form"), needs to be forms[0] for the first form or form['name']. Or you can backwards reference it from the field object as I did below :fieldObjecy.form <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <script type="text/javascript"> function disablefield(fieldObj) { var fields = new Array('att_pc', 'att_npc', 'att_beast'); for(var i=0; i<fields.length; i++) { fieldObj.form[fields[i]].disabled = (fieldObj.value!=0 && fieldObj.name!=fields[i]); } return; } </script> </head> <body> <form method="get" name="simcombat" action="http://mysite/simcombat.php?ta=20"> <table border="0" bgcolor="#D2B48C"> <tr><th colspan="4">Combat simulator</th></tr> <tr><th> </th><th>Player characters</th><th>Nonplayer characters</th><th>Beasts</th></tr> <tr> <td align="right">Attacker:</td> <td align="center"> <select name="att_pc" onchange="disablefield(this)"> <option value="0">--SELECT--</option> <option value="13">a b</option> <option value="2">Joe Blow</option> </select> </td> <td align="center"> <select name="att_npc" onchange="disablefield(this)"> <option value="0">--SELECT--</option> <option value="2">Joe Bloe</option> <option value="1">John Smith</option> </select> </td> <td align="center"> <select name="att_beast" onchange="disablefield(this)"> <option value="0">--SELECT--</option> <option value="4">Cat</option> <option value="3">Rat</option> </select> </td> </tr> <tr><th colspan="4"><input type="submit" name="submit" value="Simulate combat"></th></tr> <table> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
Darkmatter5 Posted August 5, 2009 Author Share Posted August 5, 2009 Thanks, worked great! 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.