<input type="text" id="number_one" placeholder="Please Enter a Number (1-5)" onBlur="checkInput(first)">
I’m trying to ensure the value entered is a number between 1 & 5 so I thought Id run a function using the onblur event of it
Heres the function…
var first = document.getElementById("number_one").value;
function checkInput(value) {
value = Number(value);
if(isNaN(value) || (value > 5 || value < 1)){
alert("Enter a number between 1 & 5");
}
}
But it shows the alert no matter what I enter in the text box.
I thought I set this up right?
If I’m seeing this correctly, it looks like the code logic flow is. var first = document.getElementById("number_one").value;
IOW, whatever the value of “number_one” is at that time will be assigned to the variable “first”
the input has onBlur="checkInput(first)
IOW, on blur the “first” variable will be passed to the “checkInput” function.
thanks for your help. The assignment is supposed to have two things.
Its making sure anything input into the text box is a number from 1 to 5. And when the image is clicked, it should change so the textbox has an onblur event and the image has an onclick event. I used your first script cause the validation should throw up an alert, And on the second function, I need to use an if/else statement
Heres what i have so far
<script>
(function() {
'use strict';
var images = [];
images.push("images/one.gif");
images.push("images/two.gif");
images.push("images/three.gif");
images.push("images/four.gif");
images.push("images/five.gif");
document.getElementById("dynamic-image1").onclick = function (){
flipImage1(this.src);
}
document.getElementById('number_one').onblur=function(){
checkInput(this.value);
}
function checkInput(value) {
value = Number(value);
if(isNaN(value) || (value > 5 || value < 1)){
alert('Enter a number between 1 & 5');
}
}
function flipImage1(first) {
var image1 = document.getElementById('dynamic-image1').src;
if (first == "1")
image1 = images[0];
else if (first == "2")
image1 = images[1];
else if (first == "3")
image1 = images[2];
else if (first == "4")
image1 = images[3];
else
image1 = images[4];
}
}());
</script>
then my page only has an image, and a text box
<img id="dynamic-image1" src="images/zero.gif" alt="">
<input type="text" id="number_one" placeholder="Please Enter a Number (1-5)">
Breaking it up like this seems pretty straightforward, the onblur function seems to work as expected, but nothing seems to happen when I click on the image. This doesn’t work?
document.getElementById("dynamic-image1").onclick = function (){
flipImage1(this.src);
}