JavaScript - What Is Wrong With This If Statement
I have a form and it allows someone to enter a user level, rather number 1 or 5, so I made this code to check if its 1 or 5, but it doesnt work, I get the alert everytime no matter what number I enter. I am new to javascript, but I just cant see anything wrong with this.
PHP Code: var ul=document.forms["edit"]["userlevel"].value; if (ul != 1 || ul != 5) { alert("Please enter a user level 1 or 5"); return false; } Thanks Ben Similar TutorialsHello everyone, I am using javascript and I have a radio button that the user selects and I have a function to see which button was selected, but i'm trying to use the return of that if statement in another statement. Basically another part of the function is dependent on what the user selects in the radio button. Here is what I have so far (I have some things in there that might not work because i'm trying to figure out what works): Code: <script type="text/javascript"> function getSelectedRadio() { len = document.dates.dep.length // returns the array number of the selected radio button or -1 if no button is selected if (document.dates.dep[0]) { // if the button group is an array (one button is not an array) for (var i=0; i<len; i++) { if (document.dates.dep[i].checked) { return i } } } else { if (document.dates.dep.checked) { return 0; } // if the one button is checked, return zero } // if we get to this point, no radio button is selected return -1; } function Calculate() { var i = getSelectedRadio(); if (i == -1) { alert("Please select if Marine entered Delayed Entry Program"); } else { if (document.dates.dep[i]) { return document.dates.dep[i].value; } else { return document.dates.dep.value; } } if (document.dates.yearDEAF.value < 1985 && document.dates.monthDEAF.value < 01 && document.dates.dayDEAF.value < 01 && first return from above ) { document.dates.yearDEP.value = (document.dates.yearDEAF.value); document.dates.monthDEP.value = (document.dates.monthDEAF.value); document.dates.dayDEP.value = (document.dates.dayDEAF.value); document.dates.yearPEBD.value = (document.dates.yearDEAF.value); document.dates.monthPEBD.value = (document.dates.monthDEAF.value); document.dates.dayPEBD.value = (document.dates.dayDEAF.value); } else if (document.dates.yearDEAF.value < 1985 && document.dates.monthDEAF.value < 01 && document.dates.dayDEAF.value < 01 && second return from above ) { document.dates.yearPEBD.value = (document.dates.yearAFADBD.value); document.dates.monthPEBD.value = (document.dates.monthAFADBD.value); document.dates.dayPEBD.value = (document.dates.dayAFADBD.value); document.dates.yearDEP.value = "N/A"; document.dates.monthDEP.value = "N/A"; document.dates.dayDEP.value = "N/A"; } } </script> I color coded in red the returns i'm trying to reference and where they need to be. Is this possible, and if so how can I do it? I haven't been able to find anything on the internet so far. Any help is greatly appreciated! I'm trying add the value of multiple text boxes automatically using javascript i revised the code many many times but i cant find an error or maybe im missing something you can find the source code in the link below http://wilson382.info/code.html or just download the file so you can revise and test code easily http://wilson382.info/build.zip what I want to do will work exactly like this: http://wilson382.info/src.html The working code of this script i got it from http://javascriptsource.com/ so credit goes to them thank in advance to anyone who try to help. edit: it works fine now but its not giving me the correct result the correct result is 1076 check it out: http://wilson382.info/build.html here is my code Code: <input type="button" onclick='fetch(\"no\", \"wifi.pl?action=connect&network="+document.getElementById(\'networkname\').value+"\");' value="Connect"> no matter how i try the combinations of '"\" i cannot seem to get the element value in the onclick. the fetch('no', 'url'); // no means just send the info and not get results just incase anyone was wondering. is just an ajax query Hello people, I have just started to dabble in Javascript and for my first 'project' i want to display a popup window in the center of the current browser window. I am usng the following script and when testing it using firefox/mozilla it works really well, however when i test it using Internet Explorer it goes a little wonky. here is my script: function doBubblePopUp(myId, moduleId){ var myWidth = 0, myHeight = 0; if( typeof( window.innerWidth ) == 'number' ) { //Non-IE myWidth = window.innerWidth; myHeight = window.innerHeight; } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) { //IE 6+ in 'standards compliant mode' myWidth = document.documentElement.clientWidth; myHeight = document.documentElement.clientHeight; } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) { //IE 4 compatible myWidth = document.body.clientWidth; myHeight = document.body.clientHeight; } var centerX = parseInt(myWidth / 2) - 225; var centerY = parseInt(myHeight / 2) - 275; i have looked at lots of tutorial and reference sites and with my limited understanding, the above script should work. any help would be gratefully appreciated, I realy need help what is wrong with the IF Code: <script type="text/javascript"> function Player(name, xStart, yStart){ this.x = xStart; this.y = yStart; this.name = name; this.walk = playerWalk; this.locate = playerLocate; } var urHere = new Player("You",50,50); function playerWalk(dir){ switch(dir){ case "up": this.y++; break; case "down": this.y--; break; case "left": this.x--; break; case "right": this.x++; break; default: alert("I don't understand: " + dir); } this.locate(); } if (this.y == 50){ document.write ("<h3>Blob Wins!</h3>"); } First, I am new to javascript... I've been trying to get help on a dynamic menu but no one ever seems to respond... So I will start with the pieces... I do not understand why this chunk of code is not working... All i have is a div and the script is simply supposed to make the div have a new size... here is the script... Code: <script type="text/javascript"> var stretch = 200; function Expand(index) { var dname = "menu" + index; document.getElementById(dname).style.width = stretch+'px'; } </script> here is the styling Code: <style type="text/css"> .show{height:20px; width:100px; display:block; background:lightgreen;} </style> and here is the simple html Code: <div id="menu1" class="show" onclick="runExpand(1)"></div> first- i wanted to say (for all those who see it before the Mods remove it) how stupid does a bot have to be to set its spam to be resolved at OP? lol so now my question... I have two divs with some styling. Code: #show{CSS code} #menuDet1{CSS code and visibility:hidden;} I then had in html Code: <div id="show" onmouseover="changevis(1)"></div> <div id="me1"></div> and then a function Code: function changevis(index){ acMenuDet="me" + index; document.getElementById(acMenuDet).visibility="visible"; } a colleague of mine helped me "debug" this until it worked and we came up with Code: function details_visible(index) { var acMenuDet = "menuDet" + index; document.getElementById(acMenuDet).style.visibility = "visible"; ; } function details_hidden(index) { var acMenuDet = "menuDet" + index; document.getElementById(acMenuDet).style.visibility = "hidden"; ; } Note: also changed code in div to Code: <div id="show" onmouseover="details_visible(1)" onmouseout="details_hidden(1)"> Hover over me</div> <div id="menuDet1" style="visibility: hidden;" >Plesam Plesam Plesam Plesam Plesam</div> I do not see off the bat what I was doing wrong- anyone else see what I did incorrectly? I get the 'Invalid' confirmation but not the 'Valid' one... WHY? It works when I plug in this instead though... if (user.length < 5) Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Form Validation</title> </head> <body> <script type="text/javascript"> function checkForm() { var user = document.getElementById('user').value; if (user != "turgeon"){ alert("Invalid Username"); return false; } else{ return true; } } function checkUser() { var user = document.getElementById('user').value; var element = document.getElementById('labelUser'); if(user != "turgeon"){ element.innerHTML = "Invalid Username"; element.style.color = "red"; } else{ element.innerHTML = "Valid Username"; element.style.color = "green"; } } </script> <form onsubmit="return checkForm();"> <input type="text" id="user" onblur="checkUser();" /> <label id="labelUser"></label> <input type="submit" value="submit" /> </form> </body> </html> The script i'm trying to write consist of asking the user for his name in a form format which is then sent in a cookie. When the user reconnect to the same page, it displays : Hi, "HISNAME", welcome to my site! I don't know what I'm doing wrong, seriously. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Untitled Document</title> <script type="text/javascript"> if ( ! get_cookie ( "username" ) ) { var username = document.write( <form name="name"> <table> <tr> <td valign="top"> Name : </td> <td> <input type="text" name="texte" value=""> </td> <td> <INPUT TYPE="button" NAME="bouton" Value="Click" onClick="testResults(this.form)"> </td> </tr> </table> </form>); if ( username ) { var current_date = new Date; var cookie_year = current_date.getFullYear ( ) + 1; var cookie_month = current_date.getMonth ( ); var cookie_day = current_date.getDate ( ); set_cookie ( "username", username, cookie_year, cookie_month, cookie_day ); } } else { var username = get_cookie ( "username" ); document.write ( "Hi " + username + ", welcome to my website!" ); document.write ( "<br><a href=\"javascript:delete_cookie('username'); document.location.reload ( );\"> Forget about me!</a>" ); } </script> </head> <body> </body> </html> document.write('<a href='+'"'+imagelinks[ry]+'"'+' target="_blank"><img src="'+myimages[ry]+'" border=0 max-height="250" max-width="900"></a>') I've looked over this 100 times and can't figure it out. The image shows up and you can click it. The max height and width isn't working and the image isn't opening in a new tab. Any help? I have some inline javascript/jquery that won't function when I move it from inside the body tag. I want to put it into an external file. The proper Jquery library tag is setup and external .js files are linking correctly, Am I missing something in my code? Thanks. HTML Code: <!DOCTYPE HTML> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <!--META TAGS--> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <meta name="description" content="The best of the internet all in one place" /> <meta name="keywords" content="Votillity,Homepage,Directory,email,file sharing,financial,games,jobs,language,literature,maps,music,news,politics,pro wrestling,science,shopping,social,networking,sports,travel,tv,movies,videos,calculate,calculator," /> <meta name="author" content="Matthew Chatterton" /> <meta name="google-site-verification" content="iJB8KNduVUeBu7MBY6Jyw7WqxekCCgVgwq_arLQGAZk" /> <meta name="alexaVerifyID" content="vbR_arpXTayIO5B7nHs9BjLOYFE" /> <!--STOP META TAGS--> <!--EXTERNAL FILES--> <link rel="stylesheet" type="text/css" href="css/main.css" /> <link id="altstyle" rel="stylesheet" type="text/css" href="css/main.css" /> <script type="text/javascript" src="main.js"></script> <link rel="shortcut icon" href="http://votillity.com/img/VotillityIcon.ico" /> <link rel="apple-touch-icon" href="http://votillity.com/img/VotillityAppleTouchIcon.png" /> <script type="text/javascript" src="jquery-1.4.2.min.js"></script> <!--STOP EXTERNAL FILES--> <title>Votillity.com</title> <!--CSS INTERNAL--> <!--CSS INTERNAL--> <!--JAVASCRIPT INTERNAL--> <!--JAVASCRIPT INTERNAL--> </head> <body onload="startTime()" style="background:#ffffff;"> <button class="buttontabs" style="position:fixed;top:0;left:0;" onclick="function tabs1()">Tab 1</button> <div> <object id="UUS1" style="width:100%;height:96%;visibility:hidden;" data="http://Votillity.com/Votillity.html"> <embed src="http://Votillity.com/Votillity.html"/> </object> </div> <button class="buttontabs" style="position:fixed;top:0;left:80px;" onclick="function tabs2()">Tab 2</button> <div> <object id="UUS2" style="width:100%;height:96%;visibility:hidden;" data="http://Votillity.com/Votillity.html"> <embed src="http://Votillity.com/Votillity.html"/> </object> </div> <button class="buttontabs" style="position:fixed;top:0;left:160px;" onclick="function tabs3()">Tab 3</button> <div> <object id="UUS3" style="width:100%;height:96%;visibility:hidden;" data="http://Votillity.com/Votillity.html"> <embed src="http://Votillity.com/Votillity.html"/> </object> </div> <button class="buttontabs" style="position:fixed;top:0;left:240px;" onclick="function tabs4()">Tab 4</button> <div> <object id="UUS4" style="width:100%;height:96%;visibility:hidden;" data="http://Votillity.com/Votillity.html"> <embed src="http://Votillity.com/Votillity.html"/> </object> </div> <button class="buttontabs" style="position:fixed;top:0;left:320px;" onclick="function tabs5()">Tab 5</button> <div> <object id="UUS5" style="width:100%;height:96%;visibility:hidden;" data="http://Votillity.com/Votillity.html"> <embed src="http://Votillity.com/Votillity.html"/> </object> </div> <button class="buttontabs" style="position:fixed;top:0;left:400px;" onclick="function tabs6()">Tab 6</button> <div> <object id="UUS6" style="width:100%;height:96%;visibility:hidden;" data="http://Votillity.com/Votillity.html"> <embed src="http://Votillity.com/Votillity.html"/> </object> </div> <button class="buttontabs" style="position:fixed;top:0;left:480px;" onclick="function tabs7()">Tab 7</button> <div> <object id="UUS7" style="width:100%;height:96%;visibility:hidden;" data="http://Votillity.com/Votillity.html"> <embed src="http://Votillity.com/Votillity.html"/> </object> </div> <button class="buttontabs" style="position:fixed;top:0;left:560px;" onclick="function tabs8()">Tab 8</button> <div> <object id="UUS8" style="width:100%;height:96%;visibility:hidden;" data="http://Votillity.com/Votillity.html"> <embed src="http://Votillity.com/Votillity.html"/> </object> </div> <button class="buttontabs" style="position:fixed;top:0;left:640px;" onclick="function tabs9()">Tab 9</button> <div> <object id="UUS9" style="width:100%;height:96%;visibility:hidden;" data="http://Votillity.com/Votillity.html"> <embed src="http://Votillity.com/Votillity.html"/> </object> </div> <p class="counter" style="position: fixed; bottom: -18px; right: 3px;"></p> <span class="time" style="position: fixed; top: 0px; right: 5px; font-weight: 900; color: green;" id="time"></span> </body> </html> Javascript Code: function tabs1() { var tabs1 = [ { document.getElementById('UUS2') }, { document.getElementById('UUS3') }, { document.getElementById('UUS4') }, { document.getElementById('UUS5') }, { document.getElementById('UUS6') }, { document.getElementById('UUS7') }, { document.getElementById('UUS8') }, { document.getElementById('UUS9') } ]; document.tabs1.style.visibility='hidden' document.getElementById('UUS1').style.visibility="visible" } function tabs2() { var tabs2 = [ { document.getElementById('UUS1') }, { document.getElementById('UUS3') }, { document.getElementById('UUS4') }, { document.getElementById('UUS5') }, { document.getElementById('UUS6') }, { document.getElementById('UUS7') }, { document.getElementById('UUS8') }, { document.getElementById('UUS9') } ]; document.tabs2.style.visibility='hidden' document.getElementById('UUS2').style.visibility='visible' } function tabs3() { var tabs3 = [ { document.getElementById('UUS1') }, { document.getElementById('UUS2') }, { document.getElementById('UUS4') }, { document.getElementById('UUS5') }, { document.getElementById('UUS6') }, { document.getElementById('UUS7') }, { document.getElementById('UUS8') }, { document.getElementById('UUS9') } ]; document.tabs3.style.visibility='hidden' document.getElementById('UUS3').style.visibility='visible' } function tabs4() { var tabs4 = [ { document.getElementById('UUS1') }, { document.getElementById('UUS2') }, { document.getElementById('UUS3') }, { document.getElementById('UUS5') }, { document.getElementById('UUS6') }, { document.getElementById('UUS7') }, { document.getElementById('UUS8') }, { document.getElementById('UUS9') } ]; document.tabs4.style.visibility='hidden' document.getElementById('UUS4').style.visibility='visible' } function tabs5() { var tabs5 = [ { document.getElementById('UUS1') }, { document.getElementById('UUS2') }, { document.getElementById('UUS3') }, { document.getElementById('UUS4') }, { document.getElementById('UUS6') }, { document.getElementById('UUS7') }, { document.getElementById('UUS8') }, { document.getElementById('UUS9') } ]; document.tabs5.style.visibility='hidden' document.getElementById('UUS5').style.visibility='visible' } function tabs6() { var tabs6 = [ { document.getElementById('UUS1') }, { document.getElementById('UUS2') }, { document.getElementById('UUS3') }, { document.getElementById('UUS4') }, { document.getElementById('UUS5') }, { document.getElementById('UUS7') }, { document.getElementById('UUS8') }, { document.getElementById('UUS9') } ]; document.tabs6.style.visibility='hidden' document.getElementById('UUS6').style.visibility='visible' } function tabs7() { var tabs7 = [ { document.getElementById('UUS1') }, { document.getElementById('UUS2') }, { document.getElementById('UUS3') }, { document.getElementById('UUS4') }, { document.getElementById('UUS5') }, { document.getElementById('UUS6') }, { document.getElementById('UUS8') }, { document.getElementById('UUS9') } ]; document.tabs7.style.visibility='hidden' document.getElementById('UUS7').style.visibility='visible' } function tabs8() { var tabs8 = [ { document.getElementById('UUS1') }, { document.getElementById('UUS2') }, { document.getElementById('UUS3') }, { document.getElementById('UUS4') }, { document.getElementById('UUS5') }, { document.getElementById('UUS6') }, { document.getElementById('UUS7') }, { document.getElementById('UUS9') } ]; document.tabs8.style.visibility='hidden' document.getElementById('UUS8').style.visibility='visible' } function tabs9() { var tabs9 = [ { document.getElementById('UUS1') }, { document.getElementById('UUS2') }, { document.getElementById('UUS3') }, { document.getElementById('UUS4') }, { document.getElementById('UUS5') }, { document.getElementById('UUS6') }, { document.getElementById('UUS7') }, { document.getElementById('UUS8') } ]; document.tabs9.style.visibility='hidden' document.getElementById('UUS9').style.visibility='visible' } when I click on the buttons, nothing pops up I'm not very familiar with java script at all, but could someone please explain how to make an if else else statement that follows these guidelines? 1. If the income is $70,000 or above the tax rate starts at 70% (the % sign will not show) 2. If the income is $20,000 or below the tax rate starts at 10% (the % sign will not show) 3. Otherwise the tax rate starts at 25% Thank you SO much! I've got this code below to change an images source url based on the clicks from a dropdown menu. Is it possible to shorten this with some sort of if/or statement instead of all the else if's? something that says "if crenate or serrate or this or that, do this" here's the function: Code: function change3(picName,choice) { var url = (document[picName].src); if (/crenate\D{1}/.test(url)) { url = url.replace(/crenate/, choice) document[picName].src=(url); } else if (/margin_finelyserrate\D{1}/.test(url)) { url = url.replace(/margin_finelyserrate/, choice) document[picName].src=(url); } else if (/margin_lobed\D{1}/.test(url)) { url = url.replace(/margin_lobed/, choice) document[picName].src=(url); } else if (/margin_undulate\D{1}/.test(url)) { url = url.replace(/margin_undulate/, choice) document[picName].src=(url); } else if (/doubleserrate\D{1}/.test(url)) { url = url.replace(/doubleserrate/, choice) document[picName].src=(url); } else if (/leaf_shapes\D{1}/.test(url)) { url = url.replace(/leaf_shapes/,"leaf_shapes/" + choice) document[picName].src=(url); } } The dropdown menu just has options written like this: Code: <li id="margin1"><a href="javascript:passit4('entire')" onMouseover="change1('pic8','image34')" onclick="ShowContent('uniquename3'); change3('pic2','entire');" onMouseout="change1('pic8','image_off');">sample link (entire)</a></li> Hi! I am a total newbie I'm afraid but I really enjoy learning javascript. Today I'm trying to write an if statement. I want the image1 on a web page to change when it's clicked to image2. Then I want the if statement to see if the source of the image is the changed image2.jpg (or still the original image1.jpg) and if it is the changed one I want another click to change it to image3. Here's my code, but the change to image 3 is not working: /* image change start */ var imgchanger = document.getElementById("changingimage"); imgchanger.onclick = function() { imgchanger.src = "image2.jpg"; }; /* image change 2 start */ if (imgchanger.src == "image2.jpg") { imgchanger.onclick = function() { imgchanger.src = "image3.jpg"; }; } Code: function showResults( results ) { if ( results.length == 0 ) { msg = "no projects found"; } else {msg = results [0]; } document.getElementById("RESULTS").innerHTML = msg; //= results.join( "<br/>" ); cannot get it to show "no projects found" for 0 results and also to show all multiple results if multiple are found. works right now for 1 result but not multiple. the =results.join( "<br/>" ); works if multiple but then it will not say no projects found for 0 results. Code: <script type="text/javascript"> var clicks=0; function linkClick(){ document.getElementById('clicked').value = ++clicks; if (clicks>2) { var answer = confirm ("What choice would you like?") if (answer) window.open("choiceA.html",'','scrollbars=no,menubar=no,height=600,width=800,resizable=no,toolbar=no,location=no,status=no') else (answer) window.open("choiceB.html",'','scrollbars=no,menubar=no,height=450,width=450,resizable=no,toolbar=no,location=no,status=no') } } </script> OR Code: <script type="text/javascript"> var clicks=0; function linkClick(){ document.getElementById('clicked').value = ++clicks; if (clicks>2) { var answer = confirm ("What choice would you like?") if (answer){ window.open("choiceA.html",'','scrollbars=no,menubar=no,height=600,width=800,resizable=no,toolbar=no,location=no,status=no') } else (answer) window.open("choiceB.html",'','scrollbars=no,menubar=no,height=450,width=450,resizable=no,toolbar=no,location=no,status=no') } } </script> OK, so now I'm trying so open a specific page depending on the users answer. I am aware that my code should look like this: Code: if (condition) { code to be executed if condition is true } else { code to be executed if condition is not true } However because of my click counter I have been unable to do this. My code itself does kinda work BUT choiceA open both choiceA and choiceB's pages :-( choiceB is fine and opens choiceB's page. I know that it's probably a small syntax error but I just can't see where. Thank-you in advance for your assistance. No matter what first name and last name I enter I get "Wrong!" I get wrong if I enter john smith or bill shaw Code: <html> <head> </head> <body> <script type="text/javascript"> var input; var first; var last; input = window.prompt("Please Enter First and Last Name (e.g. Joe Smith)"); if ((first == "John" || first == "john") && (last == "Smith"|| last == "smith")){ document.write ("Welcome John Smith"); } if ((first == "Bill" || first == "bill") && (last == "Shaw"|| last == "Shaw")){ document.write ("Welcome Bill Shaw"); } else { document.write("Wrong!"); } </script> <body> </html> |