[quote=“lukeurtnowski, post:3, topic:223519, full:true”]
ok, well heres what I did. I placed the at the end of the page[/quote]
That’s good with the placement of the script
I see a problem relating to this line:
var first = document.getElementById('number_one').value;
Also, the image1 variable will just contain a text string. You will need to assign the actual element itself instead, then change the src property of that element if you want to achieve anything effective.
Doesn’t my text box have the same id as the first variable?
<input type="text" id="number_one" placeholder="Please Enter a Number (1-5)">
Wouldn’t the first variable hold whatever was input into it (value)
well since im trying to target the src of the image, wouldn’t the image1 variable find that element and look up its src,
[quote=“lukeurtnowski, post:7, topic:223519, full:true”]
Doesn’t my text box have the same id as the first variable?[/quote]
Aha! The code you just now posted differs from what you had in post #3, where it had an id of “number_two”. That explains all.
Yes it does, so the image1 variable will contain a string, won’t it.
When you change that string, JavaScript doesn’t know that you want the src property of the image to be changed, does it.
Instead, you have to assign the image element itself to a variable, so that you can tell JavaScript that you want the src property of that element to be changed.
As you are only reading from that input box (not writing to, or changing it), the .value property will do swell. However, it can be better to keep it as a reference to the element, which can help other code to make more sense.
It could be improved, such as:
var index = parseInt(first.value, 10) - 1;
if (images[index]) {
image1.src = images[index];
}
function flipImage1() {
var image1 = document.getElementById('dynamic-image1');
var first = document.getElementById('number_one');
if (first.value == "1")
image1.src = images[0];
else if (first.value == "2")
image.src = images[1];
else if (first.value == "3")
image1.src = images[2];
else if (first.value == "4")
image1.src = images[3];
else
image1.src = images[4];
}
and when i enter a # into the box (2) and click the image…i still get flipImage() is undefined
I dont think I should do the index thing, the assignment want us to use an if/else statement