Jump to content

Recommended Posts

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;

}

 

 

Link to comment
https://forums.phpfreaks.com/topic/237205-switch-case-statement-not-working/
Share on other sites

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>

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.

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>

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');

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.