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

Ben Brenton
Ben Brenton
266 Points

Using HTML buttons with JS

I am working on a personal project where I want buttons on a website, and when they are clicked it loads up a JS file. I assume I need to insert code into the JS file so it doesn't jump up straight away when you load the webpage, but can anyone offer further guidance on this?

4 Answers

One way is to use the onClick attribute: http://www.w3schools.com/jsref/event_onclick.asp

Hi,

When I do my buttons. I write code in HTML and attach js file. I write function in js or jQuery in js file. It won't jump up until anybody click the button.

:)

jason chan
jason chan
31,009 Points

yup onclick. You can click buttons, p, h1, img, anything actually.

Ben Brenton
Ben Brenton
266 Points

Thanks for the help guys! I think I might be jumping ahead in my JS knowledge a bit anyway so may take a bit more time to understand how to implement in my current code, but the .onClick method certainly seems to be the way to do it.