liamloveslearning Posted May 23, 2011 Share Posted May 23, 2011 Hi, Im having trouble with a switch case statement, I've tried using if else too with no luck, In my HTML I have <select onBlur="functionCalc()" id="art"> <option value="hours" id="hours">Hours</option> <option value="minutes" id="mins">Minutes</option> <option value="seconds" id="secs">Seconds</option> </select> and the js in relation to this is // Workout if Average render time is in minutes seconds etc... switch(art) { case minutes: document.write("Finally Friday"); return false; break; case seconds: document.write("Finally seconds"); return false; break; default: document.write("Finally mins"); return false; } Quote Link to comment https://forums.phpfreaks.com/topic/237205-switch-case-statement-not-working/ Share on other sites More sharing options...
Adam Posted May 23, 2011 Share Posted May 23, 2011 Where do you define art? Quote Link to comment https://forums.phpfreaks.com/topic/237205-switch-case-statement-not-working/#findComment-1219008 Share on other sites More sharing options...
liamloveslearning Posted May 23, 2011 Author Share Posted May 23, 2011 I havent, sorry to sound naive, first proper javascript page ive written, but would I need to define art? Quote Link to comment https://forums.phpfreaks.com/topic/237205-switch-case-statement-not-working/#findComment-1219009 Share on other sites More sharing options...
liamloveslearning Posted May 23, 2011 Author Share Posted May 23, 2011 I tried defining 'art' as a variable only now its not switching, just alerting me of the variable value... var art = minutes; switch(art) { case "minutes": alert('minutes'); break; case "seconds": alert('seconds'); break; default: alert('hours'); } <select onBlur="functionCalc()" id="art"> <option value="hours" id="hours">Hours</option> <option value="minutes" id="mins">Minutes</option> <option value="seconds" id="secs">Seconds</option> </select> Quote Link to comment https://forums.phpfreaks.com/topic/237205-switch-case-statement-not-working/#findComment-1219012 Share on other sites More sharing options...
Adam Posted May 23, 2011 Share Posted May 23, 2011 Yep. Also getting the value of a select box isn't like getting the value of a text input. The options are contained within a child object of the select element called "options", and the .selectedIndex property contains the array index of the currently selected option. So you need to combine the two to get the selected option's value: var art = document.getElementById('art'); var art_value = art.options[art.selectedIndex].value; With that code, you would want to use art_value in the switch expression. Quote Link to comment https://forums.phpfreaks.com/topic/237205-switch-case-statement-not-working/#findComment-1219013 Share on other sites More sharing options...
liamloveslearning Posted May 23, 2011 Author Share Posted May 23, 2011 Ahh brilliant, I didnt realise there was an options element. Thanks a lot Quote Link to comment https://forums.phpfreaks.com/topic/237205-switch-case-statement-not-working/#findComment-1219014 Share on other sites More sharing options...
liamloveslearning Posted May 23, 2011 Author Share Posted May 23, 2011 Hi everybody, having a real nightmare with this now, I have 3 variables which take their value onkeyup from input fields on my page, I then need to multiply the 3 and output the total only im returning NaN. my complete page is <html> <head> <script> function functionCalc() { // Variables var conMin = document.getElementById('cMin').value; var serLev = document.getElementById('sLev').value; var coreHrs = noFrames * art * coresTest; var noFrames = 1800; var noFramesTot = noFrames*24; //var renHours = document.getElementById('renHours').value; var coresTest = document.getElementById('coresintest').value; var estCoreHours = 200; // Hours Minutes Seconds variables var avgframerndrtme = document.getElementById('avgrndrtime').value; var secfunc = avgframerndrtme/3600; var secmin = avgframerndrtme/60; var hourfunc = avgframerndrtme/1; var art = document.getElementById('art'); var art_value = art.options[art.selectedIndex].value; var switchart = document.getElementById('switchart').value; // Workout if Average render time is in minutes seconds etc... switch(art_value) { case "minutes": document.getElementById("switchart").value=secmin.toFixed(3); break; case "seconds": document.getElementById("switchart").value=secfunc.toFixed(3); break; case "hours": document.getElementById("switchart").value=hourfunc; break; } var total = coreHrs * serLev; document.getElementById("estDiv").innerHTML=total; alert('noframes ='+noFrames);alert('art ='+switchart);alert('cores ='+coresTest);alert('coreHrs ='+coreHrs); } </script> </head> <body> <h1>Content/SLA</h1> corehours: <input type='text' id='cHours' onKeyUp="functionCalc()" /><br /> content minutes :<input type='text' id='cMin' onKeyUp="functionCalc()" /><br /> Service level: <select onBlur="functionCalc()" onClick="functionCalc()" id="sLev"> <option value="0.84" id="mega">Priority Mega</option> <option value="0.67" id="urgent">Priority Urgent</option> <option value="0.56" id="standard">Standard Job</option> <option value="0.28" id="scheduled">Scheduled Job</option> <option value="0.14" id="lightpass">Light Pass Job</option> </select><br /> number of frames (Optional): <input type='text' id='noFrames' value="1800" /><br /> ---------------------------------------------------------------------------------<br /> <h1>render time</h1> <!--avg frame render hours :<input type='text' id='renHours' onKeyUp="functionCalc()" /><br />--> average render time :<input type='text' id='avgrndrtime' onKeyUp="functionCalc()" onBlur="functionCalc()" /> <select onChange="functionCalc()" onBlur="functionCalc()" id="art"> <option value="hours" id="hours">Hours</option> <option value="minutes" id="mins">Minutes</option> <option value="seconds" id="secs">Seconds</option> </select> <br /> cores in test :<input type='text' id='coresintest' onKeyUp="functionCalc()" /><br /><br /> ---------------------------------------------------------------------------------<br /> <h1>Estimate </h1> estimated Total : <div id="estDiv"></div><br> estimated core hours : <div id="corehours"></div> <br><br><br><br><br><br><br><br><br><br><br><br>AVERAGE FRAME RENDER TIME <input type='text' id='switchart' onKeyUp="functionCalc()" /><br /> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/237205-switch-case-statement-not-working/#findComment-1219044 Share on other sites More sharing options...
liamloveslearning Posted May 23, 2011 Author Share Posted May 23, 2011 Thinking about it, My variables get their value from an input field on keyup. Could this be the problem? if I set 'var coreHrs = 2' instead of the variables which have no value initially it works fine... Quote Link to comment https://forums.phpfreaks.com/topic/237205-switch-case-statement-not-working/#findComment-1219054 Share on other sites More sharing options...
Adam Posted May 23, 2011 Share Posted May 23, 2011 You're trying to use variables that haven't been defined yet: var coreHrs = noFrames * art * coresTest; var noFrames = 1800; [...] var coresTest = document.getElementById('coresintest').value; [...] var art = document.getElementById('art'); Quote Link to comment https://forums.phpfreaks.com/topic/237205-switch-case-statement-not-working/#findComment-1219056 Share on other sites More sharing options...
liamloveslearning Posted May 23, 2011 Author Share Posted May 23, 2011 Thanks Adam, is their a way to counter this? Quote Link to comment https://forums.phpfreaks.com/topic/237205-switch-case-statement-not-working/#findComment-1219057 Share on other sites More sharing options...
Adam Posted May 23, 2011 Share Posted May 23, 2011 Yeah, just work out "coreHrs" after you've defined the other variables. Although I'm not sure why you have art in there? The code I gave you before would mean art_value contains the value, however isn't that "hours", "minutes" or "seconds" anyway? Quote Link to comment https://forums.phpfreaks.com/topic/237205-switch-case-statement-not-working/#findComment-1219082 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.