JavaScript - Javascript Conditoinal Statemtent For 4 Conditions
Hi I am trying to write a JavaScript conditional statement for 4 conditions
I currently have: Code: var teamId=obj.id.substring(0, 1); var indx=obj.id.substring(obj.id.indexOf('_')+1); var id=indx.substring(0, indx.length-1); var anotherTab = (indx.indexOf('a') >= 0) ? id + 'b' : id + 'a'; var anotherTab2 = (indx.indexOf('c') >= 0) ? id + 'b' : id + 'c'; var anotherTab3 = (indx.indexOf('d') >= 0) ? id + 'b' : id + 'd'; document.getElementById(teamId+'tab_' + indx).className = 'selected'; document.getElementById(teamId + 'tab_' + anotherTab).className = ''; document.getElementById(teamId + 'tab_' + anotherTab2).className = ''; document.getElementById(teamId + 'tab_' + anotherTab3).className = ''; if (indx==id+'a') { show (teamId+'basketballInfo_'+id); hide(teamId + 'soccerInfo_' + id); hide(teamId + 'footballInfo_' + id); hide(teamId + 'baseballInfo_' + id); } else if(indx==id+'b') { hide(teamId + 'basketballInfo_' + id); show(teamId + 'soccerInfo_' + id); hide(teamId + 'footballInfo_' + id); hide(teamId + 'baseballInfo_' + id); } else if(indx==id+'c') { hide(teamId + 'basketballInfo_' + id); hide(teamId + 'soccerInfo_' + id); hide(teamId + 'baseballInfo_' + id); show(teamId + 'footballInfo_' + id); } else { hide(teamId + 'basketballInfo_' + id); hide(teamId + 'soccerInfo_' + id); hide(teamId + 'footballInfo_' + id); show(teamId + 'baseballInfo_' + id); } } I am trying to turn one tab on and have the others turned off until another tab is clicked on. It works fine if I have all 4 conditions but I need to be able to also have 3, 2, and 1. What would be an easy way to do so? Similar TutorialsNever mind. I found a solution to get it working. I still don't know why it occurred, but I don't have the time to fulfill my curiosity like I could when I was a teenager. I want to be able to load a different background image depending on the time of day. I have the code below which does work for nanaimo-morning image if before 8 am and after 8 am it will load the nanaimo-daytime image. I have tried but I can't seem to add another condition where after 8 pm it will load another imaged called nanaimo-night.png Yes, I am rather new at this, any help is greatly appreciated <code> <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Untitled Document</title> <script language="JavaScript"> function determineBackground() { var currentTime = new Date(); if (currentTime.getHours() < 8) { document.body.background = 'images/nanaimo-morning.png'; } else { document.body.background = 'images/nanaimo-daytime.png'; } } </script> </head> <body onload="determineBackground()"> </body> </html> </code> Hiya, Not sure how to put this, here goes: I'm trying to create a form to receive customer complaints. I'm fine with the basic form structure, but would like a drop-down box or radio buttons allowing the user to state which country in the UK they are emailing from. The options are, England, Scotland, Wales and N Ireland. For example, a user fills a form in with their complaint, and selects Scotland from the drop-down/radio buttons. When the users clicks on the submit button, the script should then send an acknowledgement email to the user, and send the form details of the user to the Scottish email address of the organisation. Likewise for England, Wales and N Ireland. I'm looking at doing this all client-side. Any help would be gratefully appreciated. Many Thanks Snarf1974 FYI: THIS IS NOT HOMEWORK! Hi all, I am trying to make an all-in-one function that can use local storage and provide 4 functions: (1) read data (2) write data (3) erase specific data (4) erase all data Here's the code block I have (with alerts in place of the local storage code for debugging): Code: var storage = function (name, value) { if (name) { if (value === null) { return alert('erase'); } else { if (value != null) { return alert('write'); } else { return alert('read'); } } } else { return alert('nuke'); } }; I want to use it like this: storage(); - erases ('nukes') everything storage('abc'); - reads data stored as 'abc' storage('abc', 'newvalue'); - stores 'newvalue' as 'abc' storage('abc', null); - erases data stored as 'abc' only It seems to work, but I have a funny feeling about trusting the difference between "==" and "===" to make it work. Am I wrong? Is there a real difference between == and === and am I doing this correctly? Thanks! -- Roger Hi! I am trying to check and see if three fields are empty and if they are then to alert the user saying to fill in one of the three fields. I tried this: Code: if (StreetNumber.value.length == 0) { if (StreetName.value.length == 0) { if (City.value.length == 0) { alert("Please enter a street number, street name, or city."); StreetNumber.focus(); return false; } } } and this: Code: if (StreetNumber.value.length == 0 && StreetName.value.length == 0 && City.value.length == 0) { alert("Please enter a street number, street name, or city."); StreetNumber.focus(); return false; } Neither one works. Not sure what I am doing wrong. Thanks in advance! Hi I have the following code. What I want to do is for certain questions to remain hidden inless certain conditions are met, then they appear to be answered. I am thinking with the <div> but not sure how. I also thought maybe with css but cant write a if statement in css so that a no go. Please help. Code: <tr><td align="left">Do you own a Yorkie?<br /> <input type="radio" name="owner" value="yes" /> Yes <input type="radio" name="owner" value="no" checked="checked" /> No<br /> <!-- If answer is no above the below questions are hidden, if yes then they appear -->> <div id="breeders"> How many? <select name="qnty" size="1"> <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></select> Are you a breeder? <input type="radio" name="breeder" value="yes" checked="checked" /> Yes <input type="radio" name="breeder" value="no" /> No</div></td></tr> JavaScript Code: I am using a onload within the form opening tag Code: function showDiv() { if(document.forms[2].owner.value == "yes") { document.getElementById('breeders').style.visibility = 'visible'; } else document.getElementById('breeders').style.visibility = 'hidden'; } Hey all, Okay, Here is what i would like to do. I have a calculator he http://www.nico-online.co.uk/calculator/calc1.html which after some help from Dormilich - got working! Now, I would like to set up some conditions, so if the use selected 'Mountainbike' then, the script will perform a different calculation to if they select the 'Rennrad' option. Okay, so I was thinking an If..else..if..else statement would probably be best after reading up This is what I have come up with: Code: <script language="JavaScript"> function Framesize() { var a = parseFloat(BikeSizer.Insleg.value); if(BikeSizer.Biketype.value = "Mountainbike") { b = (a * 0.572); c = Math.round(b); BikeSizer.FramesizeCM.value = c; d = (a * 0.226); e = Math.round(d); BikeSizer.FramesizeInch.value = e; } else if (BikeSizer.Biketype.value = "Trekking-, Reise- oder Cityrad") { b = (a * 5); c = Math.round(b); BikeSizer.FramesizeCM.value = c; d = (a * 5); e = Math.round(d); BikeSizer.FramesizeInch.value = e; } else{ b = (a * 10); c = Math.round(b); BikeSizer.FramesizeCM.value = c; d = (a * 10); e = Math.round(d); BikeSizer.FramesizeInch.value = e; } } </script> So, I'm not too sure if I can set the Menu values as recongnisable values! Here are the values I have: <select name="Biketype" style="font-size:11px;"> <option>Mountainbike</option> <option>Trekking-, Reise- oder Cityrad</option> <option>Rennrad</option> </select> Anyone out there offer any insight? Am I going horribly wrong somewhere? Apologies for the stupidness , I am quite new to JS! Thanks in advance Hello! I am trying to find a script that allows you to open multiple browser tabs and then close each of those tabs, either one by one or all at once. Does anyone know how to do this please? Thanks so much for your help. I want to have another go at Javascript. I have several books on the subject but I find that my eyesight is a major problem. Therefore I want to try an on-line solution, preferably free. I have Googled, but there are so many that I am almost dizzy with the choices. Perhaps someone could recommend one. Not too fussy visually. My knowledge is VERY basic. Frank Does anyone know how to make URL links that use Javascript still work when users have Javascript disabled on their browser? The only reason I'm using JS on a URL is because my link opens a PDF file, and I'm forcing it not to cache so users have the latest version. I tried the <script><noscript> tags, but I'm not sure if I'm using it correctly, as my URL completely disappears. Below is my HTML/Javascript code: <p class="download"> <script type="text/javascript">document.write("<span style=\"text-decoration: underline;\"><a href=\"javascript:void(0);\" onclick=\"window.open( 'http://www.webchild.com.au/mediakit/Direct_Media_Kit_Web.pdf?nocache='+ Math.floor( Math.random()*11 ) );\" >The Child Magazines Media Kit</a></span> (PDF 1 MB) ");</script> <noscript><span style="text-decoration: underline;"><a href="http://www.webchild.com.au/mediakit/Direct_Media_Kit_Web.pdf" >The Child Magazines Media Kit</a></span> (PDF 1 MB)</noscript> </p> Thanks for any help, Michael Hi, I have the following code snippet: test.html ====== <script language="javascript" type="text/javascript"> var testVariable = "test"; </script> <script language="javascript" type="text/javascript" src="test.js"> </script> test.js ===== var testVariable = window.top.testVariable; In firefox, I'm able to access testvariable defined within test.html in test.js. But in chrome, test.js couldnot get the window.top.testVariable field defined in test.html. Can any one please let me know how i can make it work in chrome?. Am i missing something here?. Hi Guys, I am new at JavaScript and start to do some tutorials.What I am trying to do here is prompting user to input a name and if the name was valid the page(document) will display with all objects like the button.But if user enter a wrong name then the button will be disabled! I create the following code but it did not work <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>New Web Project</title> <script language="JavaScript" type=""> function changeColor(){ document.bgColor = "Gray"; } </script> </head> <body> <script language="JavaScript" type="text/javascript"> var person = ""; person = prompt('What is Your Name:'); if (person == "Foo") { document.write("<h1 />Welcome " + person); document.bgColor = "Yellow"; } else { document.write("<h1 />Access Denied!!!!"); document.bgColor = "Red"; document.getElementById("gree").disabled = true; } </script> <div> <p/><input id="gree" type="button" value="Gray " onClick="changeColor();"> </div> </body> </html> as you can see I used the: document.getElementById("gree").disabled = true; but it did not work , could you please give an idea how I can solve this problem? Thanks Hey, I've got to make the values of some textboxes change the co-ordinates of my sprite on a canvas and havent a clue on how to do it, Here is my form with the two textboxes and submit button: <form> x: <input type="text" name="x" /><br /> y: <input type="text" name:"y" /><br /> <input type="submit" value="Submit"/><br /> </form> And i need it so that they change the values of these: //this shows where my sprite will start on the canvas var block_x; var block_y; searched the internet for hours and cant really find anything i understand or works. any help is much appreciated I got an index.php Code: <html> <form action="bacakomik.php" method='post'> <select name="kodekomik"> <option value='../komik1/|23'>Judul Komik1</option> <option value="../komik2/|20">Judul Komik2</option> <option value="../komik3/|10">Juduk Komik3</option> <option value="../komik4/|20">Judul Komik4</option> </select> <input type="submit" /> </form> <?php echo ('<select>'); echo ('<option value= "'.$i.'">'.'Page '.$i.'</option>'); echo ('</select>'); ?> </html> As you can see, each of the option brings specific value "../komik1/|23" komik1 is a directory | is a delimiter 23 is the pages in one chapter and can be considered also as how many images are there on a specific directory This is my bacakomik.php Code: <?php $dirkomik = $_POST['kodekomik']; $exploded = explode("|", $dirkomik); echo ($exploded[0]); //picture directory echo ("<br>"); echo ($exploded[1]); //total page in the comic $pagecount = (int)$exploded[1]; //Take last posted value, process it right away echo ('<FORM name="guideform"> '); echo ('<select name="guidelinks">'); $i=1; do { echo ('<option value= "'.$i.'">'.'Page '.$i.'</option>'); $i= $i+1; }while($i <= $pagecount); //Printing option and select echo ("</select>"); ?> <input type="button" name="go" value="Go!" onClick="document.getElementById('im').src=document.guideform.guidelinks.options[document.guideform.guidelinks.selectedIndex].value+'.png';"> </FORM> <img src="img0.jpg" id="im"> With the current code on bacakomik.php, I only can change the img src of id "im" in the same directory only. What I want is that the Javascript could "add" the "$exploded[0]" variable so that the picture can be loaded from different directory. Anyone can do this? I believe that the fix should be somewhere on input tag inside OnClick, or do you know where? Anyway, I found this on the net http://p2p.wrox.com/php-faqs/11606-q...avascript.html Please help me to those who can... I want to insert this js snippet Code: function addText(smiley) { document.getElementById('message').value += " " + smiley + " "; document.getElementById('message').focus(); return false; } to a loaded iframe with name&id chtifrm. I can access it & change embed something in its html via using something like: Code: $(parent.chtifrm.document.body).append('<div id=\"smly\" style=\"cursor:pointer;float:left;top:200px;display:none;position:absolute;\"><\/div>'); .... Code: parent.chtifrm.document.getElementById('chatbox_option_disco').style.display == 'none' but how do I insert js in the head of loaded iframe? Hi Guys I am trying to modify the functionality of my page. I want to be able to activate this piece of code using another javascript function. This is the code I want to activate: Code: <script type="text/javascript"><!-- $('#button-cart').bind('click', function() { $.ajax({ url: 'index.php?route=checkout/cart/update', type: 'post', data: $('.product-info input[type=\'text\'], .product-info input[type=\'hidden\'], .product-info input[type=\'radio\']:checked, .product-info input[type=\'checkbox\']:checked, .product-info select, .product-info textarea, .date_data input[type=\'text\']'), dataType: 'json', success: function(json) { $('.success, .warning, .attention, information, .error').remove(); if (json['error']) { if (json['error']['warning']) { $('#notification').html('<div class="warning" style="display: none;">' + json['error']['warning'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>'); $('.warning').fadeIn('slow'); } for (i in json['error']) { $('#option-' + i).after('<span class="error">' + json['error'][i] + '</span>'); } } if (json['success']) { $('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>'); $('.success').fadeIn('slow'); $('#cart_total').html(json['total']); $('html, body').animate({ scrollTop: 0 }, 'slow'); } } }); }); //--></script> And this is how I want the format of the function to be: function testsession() { if there is a session called 'hiredate' { activate the script above } else { var el = document.getElementById("product_data"); } } I just dont know how to write this in javascript Could you help me if possible please All -- I have a JavaScript config file called gameSetting.js which contains a bunch of variables which configures a particular game. I also have a shared JavaScript library which uses the variables in gameSetting.js, which I include like so: <script type="text/javascript" src="gameSetting.js" ></script> <script type="text/javascript" src="gameLibrary.js" ></script> In gameSetting.js I have: $(document).ready(function() { // call some functions / classes in gameLibrary.js } in Firefox, Safari, and Chrome, this works fine. However, in IE, when it's parsing gameSetting.js, it complains that the functions that live in gameLibrary.js aren't defined. When it gets to parsing gameLibrary.js, the variables in gameSetting.js are reported as not being defined. I've tried dynamically bootstrapping the gameLibrary file using this function in document.ready for dynamic load... $.getScript("gameLibrary.js"); However, the same problem still happens in IE, where when it parses the files individually it's not taking into context the file/variables that came before, so it's not an out of load order problem. My options a 1) collapsing all the functions in gameLibrary.js and variables in gameSetting.js into one file. However, this is not practical because this is dealing with literally hundreds of games, and having a gameLibrary.js in ONE location for ONE update is what makes most logical sense. 2) figure out a way to get this to work where variables in file1 are accessible to file2 in IE (as it seems they are in other browsers). jQuery seems to be able to have multiple plugins that all refer to the based jQuery-1.3.2.js, so I know there is a way to get this to work. Help appreciated. Nero I wrote a log function that took note of various function calls. Thinking that functions are first class objects, and objects have properties, I made the name of each logged function a property of that function, e.g., brightenInnerPara.name = "brightenInnerPara"; Every browser I tried (Firefox, MSIE, Opera, Chrome, Safari) accepted the assignment, no problem. In Firefox and MSIE, the result was what I wanted: brightenInnerPara.name == "brightenInnerPara" But in the others, the result was: brightenInnerPara.name == null Question 1. Which Javascript is correct here? I favor Firefox and MSIE, not merely because they were willing to give me what I wanted, but also because it makes no sense to accept an assignment statement without throwing an error and then give it a null semantics, like Chrome, Opera, and Safari did. I found a workaround, using assignments like this: brightenInnerPara.prototype.name = "brightenInnerPara"; To my surprise, that worked in every browser. But I don't know why. It seems that such assignments are enough to cause each function to have its own distinct prototype. Question 2. Just how inefficient is my workaround, and why does it work? OK .. Solved ... there was a typing mistake in the code Today is my first day to start learning JavaScript. When i try to run the following code (copy-paste from a book), the browser gives me an error. Can you please tell me what's wrong with the following code: <HTML> <HEAD> <SCRIPT LANGUAGE = "JavaScript"> function upperMe(){ document.converter.output.value = document.converter.input.value.toUpperCase() } </SCRIPT> </HEAD> <BODY> <FORM NAME = "converter"> <INPUT TYPE = "text" NAME = "input" VALUE="sample" onChange="upperMe()"> <INPUT TYPE = "text" NAME = "output" VALUE="sdf"> </FORM> </BODY> </HTML> Thanks Ahmad I've got a call from an HTML form to a getBoardsSold() function Code: <input value="Show Me the Money" type="button" onClick="getBoardsSold();" /> the function is Code: function getBoardsSold() { createRequest(); var url = "getUpdatedBoardSales-ajax.php"; request.open("GET", url, true); request.onreadystatechange = updatePage; request.send(null); } Why do I always receive the error Object required line 101 (refering to the function call). Seems it isn't recognizing the function mark |