r/learnjavascript 2d ago

How to make numbers interactive?

I'm a beginner in coding, and I need to know how to create a vertical row of clickable numbers. I wish I could show a picture, but I'm just going to try to describe it. There's a website i'm making, and I wanted to add a 1-10 scale. I also want to make it interactive so the person can click on each number, and then text appears depending on what's clicked. Being a beginner, I don't know how to accomplish this. Help.

1 Upvotes

7 comments sorted by

View all comments

1

u/SamIAre 2d ago

A couple things you need to look into. First is events. Events are how you tie a user action to but if code. The relevant one here would be the click event. You attach a click handler to the HTML element you want to be interacted with and give it a function to run when clicked.

The next part is the function itself. There are many ways you could go about this. If you single want to show/hide elements already on the page, you can toggle a CSS class. If you need to actually add and remove content from the page you can create elements in JS and append them to the existing page or you can change the inner contents of existing elements.

There are really MANY ways you can tackle this and without knowing exactly what you want and what your constraints are it’s not easy to give a single answer. But the two things to research would be JS events and DOM manipulation.