Jump to content

javascript doesnt work!


christiana

Recommended Posts

Hello all!i am having troubles with my javascript function!what i want to do,is to check if user selects more than one same items in my 3 select lists...my code is as follows:function checkUniquePrefs() { var list = document.getElementById("select1") var name = list.options[list.selectedIndex].text var list2 = document.getElementById("select2") var name2 = list2.options[list2.selectedIndex].text var list3 = document.getElementById("select3") var name3 = list3.options[list3.selectedIndex].text var test = document.getElementById("myform") if (name != name2 && name != name3 && name2 != name3) { test.submit(); } else { return false; } }and i call the function as follows: <input type="submit" value="Save" onClick="checkUniquePrefs()"/>what goes wrong?sometimes i think that the code doesnt run at all..what is wrong with my code??any help appreciated!!!!!

Link to comment
Share on other sites

i tried that but it still doesnt work!!!i cant understand what is the problem...my selects are as follows <select name="mySelect[0]" id="select1"> (they are filled with values from database..)can you find the mistake?it just doesnt work!i also try calling the function from a button and not an input type of submit,that is <button onClick="checkUniquePrefs();">Save</button>but it stil..doesnt work!!!

Link to comment
Share on other sites

well i now have the same id and name and i have chenged that also in fuction,but it still doesnt work.[] arent problem as far as attribute name is concerned,they worked perfectly fine..but no change makes this function work!!!!any thoughts?thanks!
Couple of things jonathan, when validating a submitted form you should use the onsubmit event handler in the form tag, not on the button itself, when you were returning false it wasn't making any difference ie the form would always be submitted. What i've done is added the variable sub which is set to false, if the conditional statement is true (no options are the same) then it sets sub to true, i then return the sub variable back to the form because it is expecting it, if the value is true the form gets submitted, if false it doesn't.Should work ok :) <html><head><script>function checkUniquePrefs() {var sub=false;var list = document.getElementById("select1")var name = list.options[list.selectedIndex].textvar list2 = document.getElementById("select2")var name2 = list2.options[list2.selectedIndex].textvar list3 = document.getElementById("select3")var name3 = list3.options[list3.selectedIndex].textif ((name != name2)&&(name != name3)&&(name2 != name3)) {sub=true;}return sub;}</script></head><body><form id="myform" action="mypage.php" method="post" onsubmit="return checkUniquePrefs()" ><select id="select1"><option value="one">one</option><option value="two">two</option><option value="three">three</option></select><select id="select2"><option value="one">one</option><option value="two">two</option><option value="three">three</option></select><select id="select3"><option value="one">one</option><option value="two">two</option><option value="three">three</option></select><input type="submit" value="Save" /></form></body></html>
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...