I am having an issue with document.getElementByID in Mozilla. In the IE and Chrome my code is working well.
I have written the following code:
<script type="text/javascript">
function test(x, y) {
var text1 = document.getElementById('text1');
for (var i = 0; i < x.length; i++) {
text1.innerText += x[i]; // prints 12345
}
text1.innerText += "\ny: " + y; // prints y: 1,2,3,4,5
}
</script>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</div>
<div id="text1"></div>
</form>
</body>
Can any one tell why this doesn’t work in FireFox?
I don’t think it’s the call to
getElementByIdthat’s the problem — if it is, then you didn’t give enough detail — but I can tell you that Firefox and some other browsers do not implementinnerText. For those browsers, you need to work withtextContentor text nodes directly.You can find out whether
textContentis supported or not using feature detection:Then, you use this variable for the property access in your code:
Feel free to shorten the variable to something you feel more comfortable with. Note that there are some minor differences in behaviour between both properties, but mostly you won’t notice them.
textContentis the standards behaviour which is why I test for that first in my code.