The elseif => else if problem, which amurra points out, is your common problem, but I would also like to suggest an alternative approach that uses an array of element identifiers, rather than if and else if s, For example:
function showDiv() { var myNumValue = document.getElementById('c').value, myNum = Math.min(Math.floor(parseInt(myNumValue, 10) / 100), 6), myDiv = ["div1", "div2", "div3", "div4", "div5", "div6", "div7"][myNum]; document.getElementById(myDiv).style.display = "block"; }
A lot tidier, right? Live example: http://jsfiddle.net/AndyE/FFeLC/1/ :-)
Please note, however, that this is not quite the same. In your example, if the value is 300, then div3 will be shown. In my example, div4 will be shown (which I think is more likely what you need). If you prefer it, as it is in your code, you can subtract any number from 0 to 0.1 from myNumValue after parsing it (and before dividing by 100) to get the same result that you expect from your code.
Also, this is probably what you really want: http://jsfiddle.net/AndyE/FFeLC/2/ .
source share