Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

dylan kane
dylan kane
2,772 Points

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>
Carlos José
Carlos José
Courses Plus Student 12,431 Points

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.

Kevin Korte
Kevin Korte
28,109 Points

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.

dylan kane
dylan kane
2,772 Points

Yes I am working with mean stack

1 Answer

Kevin Korte
Kevin Korte
28,109 Points

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.

dylan kane
dylan kane
2,772 Points

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