JavaScript - Lack Of Response In A If-else Statement
Hello,
Im just starting out with java script. I have written the following statement, and for some reason it does not follow up beyond the prompt command. As i type in yes or no - the screen empties. Id be happy if you could tell me where i had gone wrong. My guess its something about the syntax. <!DOCTYPE html> <html> <title>ex</title> <head> <script> var question1 = prompt("do you want to hear a story?"); if (question1 = "yes") {console.log ("once upon a time there was a dragon");} else { console.log ("Maybe listen to a little music?");} </script> </head> <body></body> </html> Similar TutorialsHi, I am a newbie at XML and this issue is proving to be a tough nut to crack for me. My AJAX query returns me an XML response of the following structure for a query on "ind": <countries> <country> <name>India</name> <capital>New Delhi</capital> </country> <country> <name>Indonesia</name> <capital>Jakarta</capital> </country> </countries> How do I transform the XML response into a HTML layout? Particularly, how do I get down to the value at each node?How do I traverse the XML document using JS? Is this (using XML) better than JSON (other than the obvious platform-independence)? Which is better? Thanks! Hi all, I'm new to php, but managed to get a domail.php working. My form from my html website is sent by the php file to my email address. However, the php echoes a mailresult upon successful sending, but how can I let my website respond to this mailresult?? I'd like to go to a different page, when the mailresult==OK, so something in my html file like if($mailResult==OK){ //go to page saying, your mail has been sent! } OR I could change the "OK" to something more like "Thank you, your mail has been received" in the php file, because this text IS shown in my browser window. Then I would need to know how to make the layout of the window saying this fit my website layout, because now the text is just in a white plain frame.. Hope you guys can help me with either of those two options. Thanks a ton!! Jay Hi Guys, I am having a small problem in my Javascript when I am using HTTPRequest. I am unable to recieve my Response back from server (Java Servlet) in time. I am using following code; Code: // Following function will simply goes to the Server and check if provided class is present function verifyClass(clsName) { var url = "checkServlet?className=" + clsName; var chkResult = ""; var httpReq = null; // Create an ActiVEX control object according to BRowser we are using httpReq = GetXmlHttpObject(); if (httpReq == null) { alert ("AJAX Request Failed!!! - Your browser does not support AJAX - Skipping CLASS Check."); } else { // First prepare ourself to recieve a response from server // Following code will simply wait untill server finish its response httpReq.onreadystatechange = function(){ if(httpReq.readyState == 4){ chkResult = httpReq.responseText; } } // Setup a request parameters httpReq.open("GET", url, true); // Send the request httpReq.send(null); } // Return the response recieved return chkResult; } I am using FireBug (plugin with my Firefox) to debug the HTML and I can see the response in it but when I am putting 'alert' before return the variable 'chkResult' is empty. Your suggestion will be appreciated; Thanks, -- SJunejo Hey guys, I am doing a website and the way it is suppose to work is when you click on a link there is a little triangle beside it, so when you click on it the triangle is suppose to face down but more links appear when you click on it. Anyways here is the code if someone could please tell me what im doing wrong? HTML Code: <ul class="menu" id="subnavmenu"> <li><a href="#" onClick="showHide('sublist1');return false;" id="subheading">About Us</a> <ul id="sublist1" style="display:none;"> <li><a href="#">Visit Us</a></li> <li><a href="#">Contact Us</a></li> <li><a href="#">Archives Unboxed and Revealed</a></li> </li> </ul> CSS Code: #subnavmenu ul.menu { display: block; overflow:hidden; } JAVASCRIPT Code: function showHide(element_id) { if (document.getElementById && document.getElementById(element_id) && document.getElementById(element_id).style) { var menu = document.getElementById(element_id); var arrow = document.getElementById(element_id + '_arrow'); if (menu.style.display == "block") { menu.style.display = "none"; if (arrow.src) { arrow.src = arrow.src.replace("down","right"); } } else { menu.style.display = "block"; if (arrow.src) { arrow.src = arrow.src.replace("right","down"); } } } } i have a drop down which triggers a ajax call. The ajax response is a form with various fields depending upon the drop down option selected. I have a text field in that response form which is to input the date. i have a calender widget and wish to display that as a popup with an onfocus or onclick on that text field. But becoz of the ajax response it does not let me do it. I know the problem is in the creation and destruction of the element where the calender has to be displayed so i tried putting the callender intiialization function in another function and calling that onclick but id doesnt work. below is my modified javascript code for the initialization of the calendar. Code: <script type="text/javascript"> var calendar1, calendar2, calendar3; /*must be declared in global scope*/ /*put the calendar initializations in the window's onload() method*/ function cal_init() { alert("hi"); calendar1 = new Epoch('cal1','popup',document.getElementById('calendar1_container'),false); calendar2 = new Epoch('cal1','popup',document.getElementById('calendar2_container'),false); calendar3 = new Epoch('cal1','popup',document.getElementById('calendar3_container'),false); }; </script> I have the following files in the same directory. I can get index.php displayed but when I enter anything and hit submit I cannot get the or die message to display. I have the files in the c:\xampp\htdocs\ folder and am using http://localhost/index.php from a browser. index.php: 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 content="text/html; charset=utf-8" http-equiv="Content-Type" /> <title>Username</title> </head> <body> <form action="index.php" method="POST"> Username: <input type="text" name="username"><br> <!--input is text, variable is 'username'--> Password: <input type="password" name="password"> <!--input is password, a type of text, variable is password --> <input type="submit" value="Log in"> <!--input is submit, which is a button, and text on button will read 'Log in'--> </form> </body> </html> login.php: Code: <?php $username = $POST['username']; // setting php var '$username' to equal 'username' from the index.php page (POST method) $password = $POST['password']; if ($username&&$password) //are true { $connect = mysql_connect("localhost", "root", "") //creates connection to database, we're creating a var called or die ("Couldn't connect"); //'$connect' and now we have 3 var's. 'localhost' is the host. 'root' is the //username. mysql_select_db("phplogin") or die("Couldn't find db"); } else die("Please enter a username and a password!"); ?> I'm make a ajax call to a processing PHP file that echos either "success" or an "error message". When the success is returning, i want Jquery to recognize this and then redirect the browsers instead of displaying the success message. Here are some snippets. I can only seem to parse to a blank or null. Jquery code: Code: $.ajax({ type: "POST", url: "process.php", data: wfLoginPost(), success: function(msg){ alert(msg); // both below are either null or blank //var res = $(msg).find("span.response").text(); //var res = $(msg).find("span.response").html(); if(res == 'Success'){ window.location = 'http://www.google.com'; } } }); process.php PHP Code: if($qry[0]['pwd'] == $password){ echo "<span class='response'>Success</span>"; }else{ echo "<span class='response'>Username or password did not match</span>"; } Here is the contents of the msg response from the ajax post: 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> <link rel="stylesheet" type="text/css" href="css/reset.css"/> <link rel="stylesheet" type="text/css" href="css/960grid.css"/> <link rel="stylesheet" type="text/css" href="css/style.css"/> <link rel="stylesheet" type="text/css" href="css/statusmessageQ.css"/> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <script type="text/javascript" src="js/jquery.statusmessageQ-0.5.js"></script> </head> <span class='response'>Success</span> Hello All, I have a string array in a class file that contains 5 values, which I need to display on the jsp page. but when I send it to the javascript it prints [Ljava.lang.String;@104f8b8 . what do i do about it. I am a new to javascript. Is there a way to get my array data from the response object. Please help. long story short, I have been trying to run a loop, say, 10 times and I know the result I want will be on the first request but it sends all 10 at the same time, over writing the variable for xmlhttp. discovering in my days of attempts that its just the nature of the beast, but I would like ANY way possible to get a response on the 1st pass of the loop in any way possible. But for now, here's a riddle I can't solve, how to make this function return with the response? Code: function ajaxRequest(query){ var response = 'I have not checked yet.'; var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { response = xmlhttp.responseText.trim(); } } xmlhttp.open("GET",query,true); xmlhttp.send(); return response; } It will always instantly say "I have not checked yet." when I would like the response. I'm okay with it being a 2-5 second delay as long as it doesn't pause the browser like a synchronous request. Even a delay of some sort just before the return would probably do it. In that case, I think I can get the rest. Thanks much in advance! 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 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"; }; } can someone help me with this pls: Code: function anything() { var tegenover = document.getElementById("minlas"); var septo = document.getElementById("combo_2"); //prijs verbinder per meter var serr = document.getElementById("comba_2"); //prijs verbinder per meter var seprr = document.getElementById("combb_2"); //prijs verbinder per meter var septrr = document.getElementById("combc_2"); //prijs verbinder per meter var nocho = document.getElementById("wide"); //breedte band in mm document.getElementById("totprijslas").value = Math.round(100*((parseFloat(septo.value) + parseFloat(serr.value) + parseFloat(seprr.value) + parseFloat(septrr.value)) * parseFloat(nocho.value))/1000)/100; } the value returned of totprijslas can be anything between 0 and 1000,but i want the value of "totprijslas" to be a minimum of "50",so for example if result is 30 then i need it to show 50.can someone help me with an if statement for this.thx Hello, I have just started studying JS and don't know a whole lot about it. One exercise I have been given is to use the while statement to ask the user to guess a number between one and ten. this is the code I have attempted so far but it does not work. Simple for any one who is good at this. Impossible to me. Any takershttp://www.codingforums.com/images/smilies/confused.gif <!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>While Test</title> <script type="text/javascript"> <!-- function whileTest(){ var number = 4; var answer = 0; while(number != answer ){ answer=prompt("Guesss a number between 1 and ten",""); } alert("4 is the correct answer") //--> </script> </head> <body onLoad="whileTest();"> </body> </html> I want to know how I can update my power_points field if a select option is chosen. For example: if animal guardian is chosen Power_points - (attribute_level * 1) if combined attacks is chosen power_points - (attribute_level * 1) if elemental control is chosen power_points - (attribute_level * 3) etc... Here is my current script: Code: <?php session_start(); ?> <form> <table cellpadding="3" cellspacing="3"> <tr> <th>Attribute</th> <th>Level</th> <th>Power Points:</th> <th>Character Points:</th> </tr> <tr> <td><input type="text" readonly="readonly" value="Scout/Knight Powers" size="19" name="attribute1" /></td> <td><select name="level" onclick="power(), remain()"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> </select></td> <td align="center"><input type="text" name="power_points" id="power_points" size="1" readonly="readonly"/></td> <td align="center"><input type="text" name="cp_remain" id="cp_remain" size="1" readonly="readonly" /></td> </tr> </table> </form> <form method="POST" action="test_batosai.php"> <table><tr><td> <div id="dynamicInput1"> <table cellpadding="3" cellspacing="3"> <thead> <tr> <th>Sub-Attribute</th> <th>Level</th> <th>Notes <i>(optional)</i></th> </tr> </thead> <tbody> <tr> <td> <select name="attribute[]" size="1"> <option value="1">Animal Guardian</option> <option value="2">Combined Attacks</option> <option value="3">Elemental Control</option> <option value="4">Emotional Control</option> <option value="5">Item Of Power</option> <option value="6">Knight Attack</option> <option value="7">Rejuvenation</option> <option value="8">Sailor Scout Attack</option> </select></td> <td> <select name="attribute_level[]"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> </select></td> <td><textarea name="sub_notes[]" rows="1" cols="30"> </textarea></td> </tr> </tbody> </table> </div> <div id="cloneDiv1"> </div> </td> <td> <div id="dynamicInput2"> <table cellpadding="3" cellspacing="3"> <thead> <tr> <th>Neutral Attribute</th> <th>Level</th> <th>Notes <i>(optional)</i></th> </tr> </thead> <tbody> <tr> <td> <select name="attribute2[]" size="1"> <option value="1">Acrobatics</option> <option value="2">Appearance</option> <option value="3">Art Of Distraction</option> <option value="4">Combat Mastery</option> <option value="5">Damn Healthy!</option> <option value="6">Divine Relationship</option> <option value="7">Energy Bonus</option> <option value="8">Extra Attacks</option> <option value="9">Focused Combat</option> <option value="10">Fortified Body</option> <option value="11">Heightened Negaverse Power</option> <option value="12">Heightened Senses</option> <option value="13">Heightened Scout Powers</option> <option value="14">Massive Damage</option> <option value="15">Powerful Mind</option> <option value="16">Special Attack/Defense</option> <option value="17">Speed</option> <option value="18">Strong Soul</option> <option value="19">Supernatural Training</option> <option value="20">Unique Character Attribute</option> </select></td> <td> <select name="attribute_level2[]"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> </select></td> <td><textarea name="neutral_notes[]" rows="1" cols="30"> </textarea></td> </tr> </tbody> </table> </div> <div id="cloneDiv2"> </div> </td></tr></table> <input type="button" value="Add another Sub-Attribute" onClick="addInput('dynamicInput1','cloneDiv1');"> <input type="button" value="Add another Neutral Attribute" onClick="addInput('dynamicInput2','cloneDiv2');"> <input type="submit" /><input type="reset" /> </form> <script type="text/javascript"> var counter = 1; var limit = 10000000;// Set a limit if you want to function addInput(divFrom,divTo){ if (counter == limit) { alert("You have reached the limit of adding " + counter + " inputs"); } else { var newdiv = document.createElement('div'); newdiv.innerHTML = document.getElementById(divFrom).innerHTML; document.getElementById(divTo).appendChild(newdiv); counter++; } } //function for Power Points function power() { var first,res1; //Take the value of first textbox and convert it to float first=parseFloat(document.forms[0].level.value); res1=(first)*10; //show the result in the result textbox document.forms[0].power_points.value=res1; } //end function for Power Points //function for Character Points remaining function remain() { var first,res2; //Take the value of first textbox and convert it to float first=parseFloat(document.forms[0].level.value); res2=10-((first)*4); //show the result in the result textbox document.forms[0].cp_remain.value=res2; } //end function for Character Points </script> Can anyone help? I am relatively new to JavaScript coding, and I a having some problems with an IF statement. I want the IF statement to tell me when the number is below the minimum. Code: function main (txtpamphletMin,txtpamphletMax,optQuality,selIncrement,txtResults) { var txtpamphletMin,txtpamphletMax,optQuality,selIncrement; var amount = 100; var errorMsg = ""; var results = ""; pamphletMin = pamphletMin.value; //Make sure that the minimum number of pamphlets required is entered if (pamphletMin == "pamphletMin<100") { errorMsg = "Number of pamphlets must be above 100"; } else { pamphletMin = txtpamphletMin.value; //get the number of pamphlets from the text box errorMsg = validatepamphletMin(Minimum Pamphlets); } I know that this is no the entire statement but I am just having problems with this one. Can anyone help? 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. Hello CF im wondering how could i use an if statement to have something pop up when visitors visit my site that allows them two choices. it would say something like this. Would you Like to always see the navigation Bar? then they have two options. Yes and No. then depending on what they choose it will either have the code use the absolute or the fixed positioning attribute for the whole code. Or is this just not possible? If you want me to explain it more just ask. 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! 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> |