简体   繁体   中英

How to use function inside Javascript file using NodeJS

I start to develop a website using NodeJS and Express. This website is about a Cinema and I have to develop a form which allows users to add a movie in database with informations like a list of actors. I write a simple JS script to add an input to enter a new actor and I call this function addActor() in a script called dynamic.js. Here is my code:

console.log("Launching dynamic.js !")

function addActor(){
    var txtNewInputBox = document.createElement('div');
    txtNewInputBox.innerHTML = "<input type='text' id='newInputBox'>";
    document.getElementById("actorsList").appendChild(txtNewInputBox);
}

Dynamic.js is stored in js repository so inside my app.js file I add the line:

app.use("/js", express.static("js"));

Of course I add this line in my file and I also add the button:

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

//.... some code ....//

<input type="button" name="addActor" value="ajouter" onclick="addActor()">

Here is the problem, when I click on the button, it returns "TypeError: addActor is not a function" but it prints "Launching dynamic.js." so I don't know how to use my function addActor(). (If I called directly my function in web console it works so I don't understand)

If you have a solution, I will be glad to hear it; Thanks ;)

Finally I find the problem. It's because I called my button "addActor" so it didn't distinguished my JS function and my HTML button.

<input type="button" name="addActor" value="ajouter" onclick="addActor()">

name="addActor" was the problem, now it works; Thanks for helping ;)

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