0

I have the following function that changes an named imager, depending on the number typed on the input text named textvalue. But this does not work. I am not sure if I have used parseInt correctly.

function conditioner (){

    var textvaluevariable= parseInt(document.getElementById("textvalue").innerHTML,10);
    switch (textvaluevariable) {
    case 0:
        document.getElementById("imager").src = "b1.png";
        break;
    case 1:
        document.getElementById("imager").src = "g1.png";
        break;
    case 2:
        document.getElementById("imager").src = "b1.png";
        break;
    case 3:
        document.getElementById("imager").src = "g1.png";
        break;
    case 4:
        document.getElementById("imager").src = "b1.png";
        break;
    case 5:
        document.getElementById("imager").src = "g1.png";
        break;
    case 6:
        document.getElementById("imager").src = "b1.png";
        break;
}


}
user2994762
  • 115
  • 1
  • 2
  • 10
  • Are you really only using those two images? Because in that case: `imagerElement.src = (parseInt(textvaluevariable,10) % 2 === 0 ? 'g' : 'b') + '1.png'; would seem to work. – David Thomas Sep 13 '14 at 16:16
  • yes there is no problem with images. I tried switch alert(textvaluevariable); and it showed NaN. The following answer worked well. (answer by Darek) – user2994762 Sep 13 '14 at 16:19
  • Your comment doesn't really clarify anything (or I really haven't slept enough); providing a live demo might be of use, though apparently your question has been answered by Derek..? – David Thomas Sep 13 '14 at 16:21

1 Answers1

0

Change innerHTML to value:

var textvaluevariable= parseInt(document.getElementById("textvalue").value ,10);

Reference: JavaScript: how to get value of text input field?

Community
  • 1
  • 1
Darek Kay
  • 15,827
  • 7
  • 64
  • 61