JavaScript alert() not working for document methods alert not working in document.ready

A really simple program, but I am not sure why its not getting executed.

Here's my HTML file :-

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<script type="text/javascript" src="example.js"></script>
<title>Tag Name Locator</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<p>
There are 3 different types of element in this body:
</p>
<ul>
<li>paragraph</li>
<li>unordered list</li>
<li>list item</li>
</ul>
</body>
</html>

And, here's the JavaScript:-

var listItems = document.getElementsByTagName("li");
for (var i = 0; i < listItems.length; i++)
{
alert(listItems[i].nodeName);
}

But, no alert message is show.

Please help on this.

If I just use a single alert line like

alert("Welcome!")

Its properly shown when browser is loaded.

Any answers?

Answer:1

The code in the javascript (if script included in header) fired before the DOM is fully loaded. So the listItems.length is 0 initially and hence the alert is not showing. So you need to put that javascript code in a function and call that function on onload event of the body tag. like this.

In HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<script type="text/javascript" src="example.js"></script>
<title>Tag Name Locator</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body onload="PageLoaded()">
<p>
There are 3 different types of element in this body:
</p>
<ul>
<li>paragraph</li>
<li>unordered list</li>
<li>list item</li>
</ul>
</body>
</html>

in JS

function PageLoaded()
{
   var listItems = document.getElementsByTagName("li");
   for (var i = 0; i < listItems.length; i++)
    {
      alert(listItems[i].nodeName);
    }
}
Answer:2

Add your js before closing body.

    ...
    ...
    <script type="text/javascript" src="example.js"></script>
    </body>

See what happens when you add in head http://jsfiddle.net/ManyE/

See what happens when you add in body http://jsfiddle.net/ManyE/1

Answer:3

put JS file script at bottom

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>

<title>Tag Name Locator</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<p>
There are 3 different types of element in this body:
</p>
<ul>
<li>paragraph</li>
<li>unordered list</li>
<li>list item</li>
</ul>
</body>
</html>
<script type="text/javascript" src="example.js"></script>

alert not call due js file execute first while your DOM element are not ready. Better to user jQuery for same..

Answer:4

Your javascript is being called before DOM tree is completely built. Therefore, it is not aware those li elements exist. You need to call this javascript after the Document is loaded:

window.onload = function()
{
    var listItems = document.getElementsByTagName("li");
    for (var i = 0; i < listItems.length; i++)
    {
        alert(listItems[i].nodeName);
    }
}

or as many suggested, write it at the bottom of the page, which will ensure it executes after these are loaded into the DOM.

JSFiddle: http://jsfiddle.net/hescano/KASLB/

Answer:5

I have to build a program for the employees of my company to declare days of work, vacation, days of illness etc.. My idea was to use fullcalendar drag and drop function to make something nice. Only ...

I have to build a program for the employees of my company to declare days of work, vacation, days of illness etc.. My idea was to use fullcalendar drag and drop function to make something nice. Only ...

  1. extract event from splunk
  2. extract event logs from windows
  3. extract data from event viewer
  4. extract logs from event viewer
  5. extract json from event splunk
  6. extract certificate from event log
  7. extract time from event splunk
  8. extract event logs from sap

click not working after i clear and append the html inside a div using jquery. Here is the html code <div id="divMain"> </div> <input id="btn" type="button" value="Clear&Add"/> ...

click not working after i clear and append the html inside a div using jquery. Here is the html code <div id="divMain"> </div> <input id="btn" type="button" value="Clear&Add"/> ...

  1. click not working after append
  2. click not working after ajax
  3. click function not working after append
  4. click function not working after ajax
  5. click event not working after append
  6. click function not working after ajax call
  7. jquery click not working after append
  8. jquery click not working after ajax
  9. right click not working after effects
  10. ng-click not working after append
  11. left click not working after effects
  12. button click not working after postback
  13. jquery click not working after load
  14. jquery click not working after html
  15. ng-click not working after compile
  16. ng-click not working after ng-blur
  17. button click not working after response.end
  18. button click not working after print
  19. right click not working after update

I have nested forms in an ng-repeat with a button whose click event calls a function that does a couple of things including a $scope.$broadcast. Everything in the function triggers once except for the ...

I have nested forms in an ng-repeat with a button whose click event calls a function that does a couple of things including a $scope.$broadcast. Everything in the function triggers once except for the ...

  1. angular broadcast called twice

I am using the following function(which i got from web) to resize the column in kendo ui. this is based on Index, i am looking out to see if there can be an option to select by column title or field/...

I am using the following function(which i got from web) to resize the column in kendo ui. this is based on Index, i am looking out to see if there can be an option to select by column title or field/...

  1. kendo resize columns
  2. kendo column resize event
  3. kendo autofit column
  4. kendo grid resize column
  5. kendo grid resize column programmatically
  6. kendo grid resize column angular
  7. kendo grid resize column width
  8. kendo grid resize column jquery
  9. kendo grid resize column event
  10. kendo grid resize column mvc
  11. kendo treelist resize column
  12. kendo ui resize columns
  13. kendo grid resize columns javascript
  14. kendo grid column resize not working
  15. kendo ui grid resize columns
  16. kendo grid column resize not working in chrome
  17. kendo grid auto resize column
  18. kendo grid column resize handle width
  19. kendo grid column resize false
  20. kendo grid cannot resize columns