Document scripts Collection - Loop through all <script> elements in the document, and output the id of each script: - Javascript DOM

Javascript examples for DOM:Document scripts

Description

Document scripts Collection - Loop through all <script> elements in the document, and output the id of each script:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<script id="myFirstScript">
document.write("Hello World!");
</script>//from ww w  .j  a  v  a2s.co  m

<button type="button" onclick="myFunction()">Test</button>

<p id="demo"></p>

<script id="mySecondScript">
function myFunction() {
    var x = document.scripts;
    var txt = "";
    var i;
    for (i = 0; i < x.length; i++) {
        txt = txt +  x[i].id + "<br>";
    }
    document.getElementById("demo").innerHTML = txt;
}
</script>

</body>
</html>

Related Tutorials