I am having a strange problem. I am using a php if to, depend on the result, send a 1 to a js function. If the 1 is sent, it should block a portion of html so it can be seen. The 1 is being sent correctly because it displays in the alert, but when it gets to the command
document.getElementById("countdown").style.display="block";
The code I am using is:
PHP:
if ($Grade == "Kindergarten")
{
echo "<script language=javascript>showCountDown(1)</script>";
}
The code in my js file is:
function showCountDown(index)
{
alert("Checking for Kindergarten");
if (!index)
var n = document.getElementById("Grade").selectedIndex;
else
var n = index;
alert("Showing Index Selected: " + n);
if (n == 1)
{
alert("about to show Countdown to Kindergarten");
document.getElementById("countdown").style.display="block";
alert("Showing Countdown to Kindergarten");
}
else
{
alert("About to remove Countdown to Kindergarten");
document.getElementById("countdown").style.display="none";
alert("Removing Countdown to Kindergarten");
}
}
Here is the strange part, at least to me, the same code works when called from the html:
<select size="1" name="Grade" id="Grade" onChange="showCountDown();">
I would like to, if possible, is Grade is "Kindergarten", display the div countdown. But, I do not understand why the code is not working correctly when the correct variable is being sent.
Can someone help?
Did you try to look if there is any javascript error on the browser's console? It should give you some clues about the error..
If you have any reference error to showCountDown
this could be because:
1 - You forgot to include your javascript file. You can do this on the php like this:
echo "<script src='your-file.js'></script>";
2 - You are calling the function before it is being declared
3 - You have other error(s) on the javascript file
As you said you weren't having troubles calling the function on the HTMl, I suspect that is the second option.