When do I use HTML or Javascript? [closed]

I am learning to code and I am in doubt about using HTML or JavaScript. For me, there are some things that JS does that is simpler with HTML. For example:

  • The action of submitting a form in html I can do with the tag "<input type= 'submit'>" and add an action. What is the advantage of using "example.addEventListener('submit', function(e)...)" ?

  • If I want to put a link on my website, I can do that with the tag <a href="website url">link</a>. Why would I use something like window.open("website url")?

I don’t know if my question was clear, but basically I am trying to understand if there is a “best practice” or if JS enables me doing something that HTML doesn’t in this cases.

When I am coding, it is better if I do things more simply, right?

Thanks a lot!!

Answer

The action of submitting a form in html I can do with the tag “” and add an action. What is the advantage of using “example.addEventListener(‘submit’, function(e)…)” ?

Sometimes, you need to do something programmatically with the form data. For example, rather than submitting the form directly to a URL, JavaScript can handle the form data and load data from the server.

If I want to put a link on my website, I can do that with the tag link. Why would I use something like window.open(“website url”)?

You usually wouldn’t. If you have a specific example, please share it, as there may be an edge case.

Leave a Reply

Your email address will not be published. Required fields are marked *