I have this script to remove a “button” id when a certain word (“registered”) is in a table cell.
The code:
<script>
var allTableCells = document.getElementsByTagName("td");
for(var i = 0, max = allTableCells.length; i < max; i++) {
var node = allTableCells[i];
var currentText = node.childNodes[0].nodeValue;
if (currentText === "registered")
document.getElementById("button").remove();
}
</script>
It works fine in almost every browser but in some mobile version in iPhone, Nokia and some others in Windows mobile. I have tested it including some modifications with no result.
Could someone tell me if there is some mistake or whatever might be causing this failure?
The code looks good. Does a test page with only the above code (and some td’s to test with) also fail?
If so, can you give us more information about the specific environments in which it does fail.
Whilst it may not be the cause of the issue, I’d suggest wrapping curly braces ( {} ) around your if statement like so…
<script>
var allTableCells = document.getElementsByTagName("td");
for(var i = 0, max = allTableCells.length; i < max; i++) {
var node = allTableCells[i];
var currentText = node.childNodes[0].nodeValue;
if (currentText === "registered") {
document.getElementById("button").remove();
}
}
</script>
If those mobile browsers don’t have trouble with the test page, then the most likely cause is that it’s something else on the more complex page that’s failing, preventing JavaScript execution from carrying on to execute the above code.