You're cases are all ints, everything that comes from the dom will be a string, so you'll have to fix that:
var d = +(document.getElementById('test').value);//+ converts to number
Be sure there are no characters in there, though as +someString will return NaN when the string looks like 123fdsf. parseInt can deal with numbers, followed by chars, but isn't all too friendly with strings with leading spaces and leading zeroes (interprets input as octal), so preferably use parseFloat if its likely the input will contain chars/leading zeroes.
Alternatively, quote your cases, and provide a defaul case:
case '6': x = 'Today is Saturday'; break;
default: x = 'Don\'t know what '+d+' is';
So you can see what value you passed to the switch
In response to @KooiInc Let's simplify: let's optimize and, admittedly, complicate (but improve performance):
Closure: (dom needs to be loaded first here)
var test = (function(dayArray)
{
var out = document.getElementById('box3');//only search dom once
var in = document.getElementById('test');
return function()
{
var day = dayArray[in.value] || 'Don\'t know day ' + in.value;
out.innerHTML = 'Today is ' + day;
}
})('Sunday,Monday,Tuesday,Wednessday,Thursday,Friday,Saturday'.split(','));
function properties:
function test()
{
if (!test.inElem)
{
test.inElem = document.getElementById('test');//search dom on first call
}
if (!test.outElem)
{
test.outElem = document.getElementById('box3');
}
if (!(test.days instanceof Array))
{
test.days = 'Sunday,Monday,Tuesday,Wednessday,Thursday,Friday,Saturday'.split(',');//create array once
}
var day = dayArray[test.inElem] || 'Don\'t know day ' + test.inElem;
test.outElem.innerHTML = 'Today is ' + day;
}
These examples are just for fun. I know premature optimization is the root of all evil, so don't worry about it that much at this stage. Just know that functions are objects, so you can set properties, that remain in memory, even after the function has returned, and know that closures can do that, too (and Sóó much more).