Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

How to link javascript events to html buttons

I need to have a javascript event linked to an html button where when it is clicked it adds a value to a mongo database. Any tips and what I should do?

u can do this in html directly

<button  onclick = "function()">Submit</button>

You need a Back-End language if anything. Because the info has to be sent to the server that will in turn do things in a database.

I'm assuming the backend language here would be node.js Carlos José based on him mentioning mongo. My guess is he's working in the MEAN stack.

Yes I am working with mean stack

1 Answer

Wouldn't that button just be a "submit" button for a form, and than at that point, you're just doing a standard ajax form submit with js.

Even if you didn't want or need any user fields, the form could have hidden fields that still submit.

No it is not a standard submit button, it is adding a specific string to the database, like if you pressed the button it would put a word into a document on mongodb