JavaScript - If Statement Doesn't Recognize Value Of Variable.
Here's my script wher problem occurs:
Code: $.post('do/register.php?username='+('#username').value, function(result) { if(result == 1) { $('#message').html("Everything's ok"); } else if(result == 2) { $('#message').html("Username is missing."); } else { $('#message').html("What the hell is wrong here?!"); } }); If i put alert(result); in function(result){ } part, i get "result" value that is '1' and that is correct value but it seems like IF statement is not recognizing it at all... #message div constantly shows me "What the hell is wrong here?!" message... Anyone know what could cause this problem? Thanks in advance, phpStud. Similar TutorialsIve searched on this board but cant find out why this doesn't work. Code: <script type="text/javascript"> function showSelected(val) if (val == "Nil") { document.getElementById('JustText').innerHTML='hi' } else { document.getElementById('selectedResult').innerHTML="<a href='mailto:" + val + "'>" + val + "</a><p>" } </script> I have a page with a Geo IP redirect that's supposed to redirect users from London to URL#1 and the rest to URL#2. It's an external geo ip lookup service. First comes the IP lookup: Code: <script language="JavaScript" src="http://j.maxmind.com/app/geoip.js"> /* GeoIP Deny Access by City and Redirect Javascript 1.0 http://wiki.category5.tv/MaxMind_GeoIP_API API (c) MaxMind - www.maxmind.com - used with permission "GeoIP Deny Access by City" script by Robbie Ferguson, www.Category5.TV You are free to use and share this script, however this notice must remain intact. */ </script> And then, and here's the problem I think, is the redirects inside an if/else: Code: <script type="text/javascript"> var city=new Array("London, H9") var redirect="http://www.URL1.com" var redirect2="http://www.URL2.com" /* do not edit past this line */ Array.prototype.inArray = function(q) { for(i in this) { if(this[i].toUpperCase() === q) return true; } } var myCity=geoip_city().toUpperCase() var myRegion=geoip_region().toUpperCase() if(city.inArray(myCity+", "+myRegion)) { window.location = redirect; } else { window.location = redirect2; } The redirect works if you are indeed from London. So if the if-statement is true, "window.location = redirect" works, but if the statement is not true, "window.location = redirect2" doesn't seem to be called. Help would be extremely appreciated Hello. Can someone please be so kind to answer the above question. Here is the code: Code: if (document.getElementById("os0").value=="1010") { price_per_inch=1.90; 'document.write(price_per_inch);' } else if (document.getElementById("os0").value=="1020") { price_per_inch=2.00; 'document.write(price_per_inch);' } else if (document.getElementById("os0").value=="1030") { price_per_inch=2.10; 'document.write(price_per_inch);' } else if (document.getElementById("os0").value=="1040")'well then 1040 is chosen' { price_per_inch=2.20; 'document.write(price_per_inch);' } else { } It is setting the price per inch based on the selection the user makes from a drop down menu. I need the correct section to work and for the else to not execute. After all, it never should, because there are only 4 options to choose from, and one of the 4 will always be chosen. If I uncomment the document.write lines after each option, things work o.k. So, what's going on? On a side note, has any ever gotten any kind of programming help from the chatrooms at Gogloom he http://gogloom.com/DIR?cat=84126&catdesc=Programming I ask because about three times I have gone there hoping to receive immediate help, but no one ever responds, hardly anyone chats, and yet the rooms are filled like with 300 and something users. I just find it odd and if nothing is wrong, how can so many people be so unhelpful? This is my first time here and hopefully when I have future problems I can count on returning here. Hey everyone, I'm a newbie writing a tic tac toe program using OOP. It was simple setting it up so that a player could click on a box to put an X or an O there, but I've run into serious issues when trying to make it so a player couldn't overwrite the AI's choice and the AI couldn't overwrite the player's. An example of this would be if I made the top right box an X and the AI then made the center box an O, and then I accidentally clicked the center box and made it into an X. I want to prevent that. Every box on the grid is an object with the property of "taken" that helps the program know if a box is empty or not, so as to avert any overwriting in the first place. If the box is empty, this.taken = 0. If the box is filled by a player, taken = 1. If filled by AI, taken = 2. I made it matter whether it was AI or human so later i can check if one of them got tic tac toe. Anyway, by default the constructor class sets this.taken = 0. But the method for checking availability and writing the X or O uses a switch which checks if taken = 0. If it is, it sets taken to either 1 or 2 and writes the necessary symbol. If taken = 1 or 2, it just alerts the player that the spot is taken. But for some reason the switch statement can't tell when taken = anything other than 0. It always executes the code for 0 even though the code for 0 inherently makes it so that taken never equals 0 again, which means case 0 cannot happen anymore. Below is the code with notes. Code: function main(input){//start of main function// function Click(who, where, what){ //this is the method for checking if it's taken or not and writing the X or O if it isn't// /*the argument who represents a number. 0 is no one, 1 is human, 2 is AI; where is a string literal representing the spot on the grid*/ switch(this.taken){ case 0: this.taken = who; document.getElementById(where).innerHTML = what; break; case 1: alert("this spot is taken"); break; case 2: alert("this spot is taken"); }//end switch }//end Click function Box(inputhere){//start of class// this.taken = 0; this.pick = Click; } //end of Box class// //object declarations (I cut out most of them and left only the relevant ones// var topleft = new Box(); var topmid = new Box(); var topright = new Box(); var centerright = new Box(); //end of object declarations// switch (input){ /*in each .pick(), the first arg is whether or not a player chose it (1 = player did, 2 = comp did). The second arg is which box and the third is whether to put an X or O. The input variable in the switch statement is an argument passed through main() when the player clicks on a box. Topleft passes 1.1, topmid passes 1.2 and so on.*/ case 1.1:{ //The first instance of .pick() in each case is what the player did. The second is what the AI will do in repsonse.// topleft.pick(1, "topleft", "X"); topmid.pick(2, "topmid", "<span>O</span>"); break; }//end of case 1.1 case 1.3:{ topright.pick(1, "topright", "X"); centerright.pick(2, "centerright", "<span>O</span>"); break; }//end of case 1.3 }//end of switch }//end of main// Is there anyone who has any idea why on earth this is happening? I've been at it for an embarrassing amount of hours. Also, thanks to anyone who even considers helping : ) (feel free to flame me if my code sucks or my post is too long or anything). Hello All, I am a beginner javascript student and am hoping to ask for some help with a problem i am having on an assignment. I have been assigned the old Pizza Form tutorial. My form asks for customer info, select size with radio button, and select toppings with checkboxes. I created a ValidateForm() function to check if these are filled in and output a message for each field saying "please fill in field." My problem now is that on Submit I need to print out a message with customer info, selected size, and selected toppings. I have the customer info done with a var message: var message = "Name: " + name + "\n"; message += "Address: " + address + "\n"; message += "City: " + city + "\n"; message += "State: " + state + "\n"; message += "Zip: " + zip + "\n"; message += "Phone: " + phone + "\n"; message += "Email: " + email + "\n"; I know I need to add IF statements to this for the sizes and toppings but do now know where to put them. here is my whole script if that helps: function validate_form() { valid = true; if (document.PizzaForm.customer.value == "" ) { alert ( "Please fill in the 'Name' box." ); valid = true; } if (document.PizzaForm.address.value == "" ) { alert ( "Please fill in the 'Address' box." ); valid = false; } if (document.PizzaForm.city.value == "" ) { alert ( "Please fill in the 'City' box." ); valid = false; } if (document.PizzaForm.state.value == "" ) { alert ( "Please fill in the 'State' box." ); valid = false; } if (document.PizzaForm.zip.value == "" ) { alert ( "Please fill in the 'Zip' box." ); valid = false; } if (document.PizzaForm.phone.value == "" ) { alert ( "Please fill in the 'Phone' box." ); valid = false; } if (document.PizzaForm.email.value == "" ) { alert ( "Please fill in the 'Email' box." ); valid = false; } if ( ( document.PizzaForm.sizes[0].checked == false ) && ( document.PizzaForm.sizes[1].checked == false ) && ( document.PizzaForm.sizes[2].checked == false ) && ( document.PizzaForm.sizes[3].checked == false ) ) { alert ( "Please choose your Size" ); valid = false; } if ( ( document.PizzaForm.toppings[0].checked == false ) && ( document.PizzaForm.toppings[1].checked == false ) && ( document.PizzaForm.toppings[2].checked == false ) && ( document.PizzaForm.toppings[3].checked == false ) && ( document.PizzaForm.toppings[4].checked == false ) && ( document.PizzaForm.toppings[5].checked == false ) && ( document.PizzaForm.toppings[6].checked == false ) && ( document.PizzaForm.toppings[7].checked == false ) ) { alert ( "Please choose your Toppings" ); valid = false; } return valid; } function orderDetails() { var name = document.PizzaForm.customer.value; var address = document.PizzaForm.address.value; var city = document.PizzaForm.city.value; var state = document.PizzaForm.state.value; var zip = document.PizzaForm.zip.value; var phone = document.PizzaForm.phone.value; var email = document.PizzaForm.email.value; var message = "Name: " + name + "\n"; message += "Address: " + address + "\n"; message += "City: " + city + "\n"; message += "State: " + state + "\n"; message += "Zip: " + zip + "\n"; message += "Phone: " + phone + "\n"; message += "Email: " + email + "\n"; alert(message) return; } function doSubmit() { if (validate_form() == true) { orderDetails(); } } Thank you! I am trying to get the variable extraCharge to increment by $1 for every topping over the 3 included toppings. How can this be done? For statements confuse me 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>Checkbox Toppings</title> <script type="text/javascript"> function validate(form) { var toppingsArray = document.form1.scripts.length; var extraCharge = 0; // Checking if at least one period button is selected. Or not. if (!document.form1.orderType[0].checked && !document.form1.orderType[1].checked) { alert("Please Select orderType"); return false; } var total="" for(var i=0; i < document.form1.scripts.length; i++) { if(extraCharge >= 3) alert("You can only select 2"); else if(document.form1.scripts[i].checked) total +=document.form1.scripts[i].value + "\n" } for(var i=3; i >= document.form1.scripts.length; i++) { var extraCharge = extraCharge + 1; } if(total=="") alert("select Toppings") else document.form2.checklist.value=(total); document.form2.toppingCharge.value=(extraCharge); return false; } </script> </head> <body> <h2>3 Toppings included, $1 for each additional topping</h2> <table width="333" border="0"> <tr> <td width="179"><form name="form1" method="post" action="" onchange="return validate(this)"> Select Order Type First:<br /> <input name="orderType" value="delivery" type="radio">delivery </font> <br /> <input name="orderType" value="takeOut" type="radio"><font face="verdana" size="2">takeOut</order></b> <br /> <br /> <br /> <input name="scripts" value="extracheese" type="checkbox">Extra Cheese <br /> <input name="scripts" value="sausage" type="checkbox">Sausage <br /> <input name="scripts" value="peperoni" type="checkbox">Peperoni </font> <br /> <input name="scripts" value="bacon" type="checkbox">Bacon <br /> <input name="scripts" value="canadianBacon" type="checkbox">Canadian Bacon <br /> <input name="scripts" value="mushroom" type="checkbox">Mushroom </font> <br /> <input name="scripts" value="pineapple" type="checkbox">Pineapple </font> <br /> <input name="scripts" value="onion" type="checkbox">Onion <br /> <input name="scripts" value="olive" type="checkbox">Olive <br /> <input name="scripts" value="greenPepper" type="checkbox">Green Pepper </font> </form> </td> <td width="144">Selected Toppings: <br /> <form name="form2"><textarea name="checklist" cols="20" rows="15" value="total"></textarea> Extra Topping Charge: <input name="toppingCharge" type="text" /> </form></td> </tr> </table> </body> </html> Hi, I am using jQuery for the following scenario: There is a random number of divs, each sharing the same class (.slecteable), each having its own id (a unique string extracted from the database). A user clicks on a div. I get the id, and change the color of this div (in gets highlighted). Code: $('.selectable').livequery('click',function(){ var id; id = $(this).attr('id'); //change color based on id. }); Now comes the problem. Once a user clicks again on a div, if it wasn't highlighted, it should return to its original color. If it had been highlighted, and had returned to its original color due to a second click, it should get highlighted again. How would you do that? I wish I could post some code, but at this point, it would be rubbish. Thanks in advance. Regards, -jj. i have this javascript code that IE wont recognize any advise please? Message: Could not get the type property. This command is not supported. Line: 263 Char: 4 Code: 0 Code: function togglePass() { var obj = document.getElementById('pass1'); if (obj.type == 'text') { obj.type = 'password'; } else { obj.type = 'text'; } } Thank you Hi there, If I have Ajax bring back a string of HTML, let's say to replace a whole <select> box, does that get registered in the DOM in such a way that I can later manipulate that element via Javascript? It doesn't seem so. I have a <select> box that gets inserted via Ajax but I have a script I want to use to change which <option> is selected after it is loaded in. The client doesn't seem to pick up on the <select> box when I try to call it via getElementById, etc. I have a div whose CSS I'm setting as follows: Code: div.services_menu { display: none; position: absolute; top: 215px; left: 240px; overflow: hidden; } I also have a initPage function that gets called on pageload: Code: function initPage() { var menu = document.getElementById("services_menu"); var menuItems = menu.getElementsByTagName("div"); alert("top = " + menu.style.top); } The above CSS applies to menu. The alert box tells me: top = So menu.style.top is not set to anything even though I explicitely set it in my CSS. Why is this? Note: I've verified that menu.style.top is still blank even well after the page loads, just to be sure that it's not an issue with CSS being read/processed after the initPage script is read/processed. It came up as unrecognizable as an MD5 hash and it looks like hexadecimal to me. Can someone help me convert it to what it is supposed to say? I think it is a series of numbers but am not entirely sure. Thank you for your assistance. 86a032c42beb25042d8d4dc7970ccc1e Hi, it's my first post and first thread, actually I joined to ask this question. I did try searching first but for some reason when I put this character in google search fields I never find what I want, maybe not being recognized. here's the code Code: this.size=a.size||2000; this.handle_event=a.handle_event||'click'; my question ... what is the || character(s)? I'm sure this must be laughably simple and thanks in advance (new to learning js) I am working on my personal portfolio site, and am using a code that will make each portfolio piece appear in a new div when the name of the piece is clicked on. The problem is, JS does not seem to recognize double digits. I am not familiar with JS at all, I just got comfortable with CSS/HTML a few weeks ago! I am in over my head. It would really, really help if someone could show me how to change the code so that I could make about 15 to 20 divs instead of 9. Here is the code: Code: <script language="JavaScript"> numdivs=9 IE5=NN4=NN6=false if(document.all)IE5=true else if(document.layers)NN4=true else if(document.getElementById)NN6=true function init() { showDiv(0) } function showDiv( which ) { for(i=0;i<numdivs;i++) { if(NN4) eval("document.div"+i+".visibility='hidden'") if(IE5) eval("document.all.div"+i+".style.visibility='hidden'") if(NN6) eval("document.getElementById('div"+i+"').style.visibility='hidden'") } if(NN4) eval("document.div"+which+".visibility='visible'") if(IE5) eval("document.all.div"+which+".style.visibility='visible'") if(NN6) eval("document.getElementById('div"+which+"').style.visibility='visible'") } </script> Thank you for taking the time to read this! Hopefully someone can help. Hi there! OK I need a little help as I'm not sure how to integrate this function into my form. First of all I should explain that my form doubles as a Google Search box. If you select the Google Search radio button it submits the input text to Google. It also defaults to Google if anything else other than "red", "green" or "blue" are entered. Now what I'd like to do is add a function so that if someone enters "purple is my favorite color" it recognizes the "purple" part of the string using something like .indexOf('purple'); and calls something like if(function==-1) { theForm.action = "Other choice "+ q.value; . How can I add this to my form? Thank you for any help. My form code: Code: <script> function convert(theForm){ var els = theForm.getElementsByTagName('input');// store inputs var q = els[0], color = els[1]; if(els[4].checked){// submit to google theForm.action = 'http://www.google.com/search'; return true; }; color.value = "Chosen "+ q.value +" color"; switch(q.value){// check the query case 'red': case 'green': case 'blue': location.href = color.value; return false; break; default: theForm.action="http://www.google.com/search"; break; }; return true; }; </script> <form name= "myform" onsubmit='return convert(this)'> <input name='q' type='text' /> <input name='color' type='hidden' /> <input type='submit' value=" Enter " /> <br /> <input name="searchType" type="radio" value="" checked="checked" > <label for="all"> My Form </label> <input name="searchType" type="radio" value="google" onclick="document.myform.action='http://www.google.com/search';" /> <label for="google"> Google Search </label></form> Hello 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! Hi, I have a programing problem that have been around for ages. I have search on google using several expressions and words and after hours of digging i'm still unable to do it. I would like to get a value from a HTML page hosted remotely with an inconstant value. Then define this value and name as a javascript variable and apply or show in my page. Thanks for all the help P.S. Is there any way to make a domain lookup in javascript? I mean a user enters a domain and the script converts to an ip and shows to the user. If not thanks, probably it can only be done in the server side... Hello I have a php page in which I declared a js variable... right at the top of the page... <script type="text/javascript"> var tester = 0; </script> Later in the page I test this variable... <script type="text/javascript"> if (tester==1){ do some stuff!! } </script> But I keep getting an error variable _tester undefined Am I missing something obvious... I am new to js... but this seems really straightforward I don't know how I should do ? Quote: <html> <head> <script type="text/javascript"> function add(a,b){ y=a+b return y } var aaa = add(one,two) //one needs to get somehow get the value of yil, in this case 10 var one = function reas(){i=10;if(i<10){yil = 15}; else{yil = 10; yil = one;}; var two = 20; document.write(y) </script> </head> </html> also why doesn't this work? Quote: <html> <head> <script type="text/javascript"> function adder(a,b){ var k=a+b; return k } var hes=adder(5,kol); kol=40; alert(hes); </script> </head> </html> you cannot have variables in callback functions? |