简体   繁体   中英

Trying to display data from a database in HTML tags using Javascript

I'm trying to call a function in my home page that will change the content of an article in HTML from placeholders to info from a database before the page loads. However I keep getting an error saying that I'm missing a formal parameter. What have I done wrong?

The HTML:

<article id="article1">
    <img src="images/sadface.png" alt="The image didn't load, sorry" id="img0">
    <h4 id="heading0">Not working</h4>
    <p id="p0">This item hasn't loaded, sorry</p>
    <script>
        window.onload = function splashFunction(0);
    </script>
</article>

And the Javascript:

function splashFunction(x){

document.getElementById("img" + x).src="images/cart2.jpg";

document.getElementById("heading" + x).innerHTML="cart";

document.getElementById("p" + x).innerHTML="cart";
};

In order to change your DOM with a call to your database you will have to use AJAX , I hope you know that.

As for your code, you are defining the function again on window.onload . To call the function just use its name, as such:

window.onload = splashFunction(0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM