I know, it's not supported by IE, but I found a cool script online that someone was generous enough to provide for free, but I can't figure out why it's not working. I've been staring at this for hours, please point me in the right direction!
My code:
<script language="javascript" type="text/javascript" src="getbyclass.js"></script>
<script type="text/javascript" language="javascript">
function editToggle(toggle){
if (toggle == "off"){
getElementsByClassName("editp").style.display ="none";
document.getElementById('editToggle').innerHTML="<a href=\"#\" onclick=\"editToggle(\"off\"); return false;\">>Edit Mode: <span style=\"color:red;\">OFF</span></a>";
toggle="on";
}else{
getElementsByClassName("editp").style.display ="inline";
document.getElementById('editToggle').innerHTML="<a href=\"#\" onclick=\"editToggle(\"on\"); return false;\">>Edit Mode: <span style=\"color:green;\">on</span></a>";
toggle="off";
}
}
also:
echo "<span id=\"editToggle\"><a href=\"#\" onclick=\"editToggle(); return false;\">Edit Mode: <span style=\"color:red;\">OFF</span></a></span>";
The code from getbyclass.js
can be seen here.
In response to the answers below, I've tried this:
function editToggle(toggle){
var list = getElementsByClassName("editp");
if (toggle == "off"){
//getElementsByClassName("editp").style.display ="none";
for (index = 0; index < list.length; ++index) {
list[index].style.display ="none";
}
document.getElementById('editToggle').innerHTML="<a href=\"#\" onclick=\"editToggle(\"off\"); return false;\">>Editfalse;\">Edit Mode: <span style=\"color:red;\">OFF</span></a>";
toggle="on";
}else{
//getElementsByClassName("editp").style.display ="inline";
for (index = 0; index < list.length; ++index) {
list[index].style.display ="inline";
}
document.getElementById('editToggle').innerHTML="<a href=\"#\" onclick=\"editToggle(\"on\"); return false;\">>Editfalse;\">Edit Mode: <span style=\"color:green;\">on</span></a>";
toggle="off";
}
}
But it's still not working.
getElementsByClassName
returns aNodeList
(or an array if it's not built-in), but you're using it as though it were anHTMLElement
by referring directly to astyle
property on it:You should be seeing an error in the JavaScript console, since you're trying to retrieve the property
display
fromundefined
(sincegetElementsByClassName("editp").style
will beundefined
).If you want to act on the first matching element:
...or if you want to act on all of them:
Update:
At some point, you edited the question and removed
var toggle = "off"
from the code (at global scope, just above the function) and madetoggle
an argument toeditToggle
. But you're not passing anything intoeditToggle
according to your quoted markup, and even if you were, settingtoggle
to a new value within the function won't have any lasting effect if it's a function argument, as nothing refers to it after the function returns.getElementsByClassName
returns a collection. You might need to loop through the results, like this:Should go through this.
You seem to have a missing semicolumn after
var toggle="off"
.Make sure that you call
editToggle()
somewhere in your code.I advise you to use inspectors built into browsers or extensions. For example Firebug extension for Firefox or Chrome Inspector. Use the console to debug and see if there are errors in your javascript.
There may be unterminated string literals in the markup you create. It also appears there may be other issues as mentioned in other posts.
Change:
to
This situation is also present in the other markup you create.
Change:
to