I recently tried to re-use a (working!) javascript function and detected a strange behaviour: After a certain if statement within a for-loop the function stopps. I don't see any reason.
If a change the actual if condition to "1==1", it all works fine. But with the code being as above I get the first alert ('here we go') but I never get the second alert. So, the problem can't be connected witht the loop (being endless or something like that). But other than that I'm absolutely confused and helpless. (Tested with Firefox 26 and Internet Explorer 11)
<!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
function my_function(number_of_divs)
{
for (var k = 0; k <= number_of_divs; k++)
{
var index = k;
if (document.getElementsByName("text_levels")[index].style.display == 'block')
{
alert('Here we go');
}
}
alert('This is never shown');
}
</script>
</head>
<body>
<a href="javascript:void" onclick="my_function(1)">Click</a>
<div name="text_levels" style="display:block">The content of a div element</div>
</body>
</html>