Interactive Rating Component using SASS, CSS Flexbox and JavaScript
Design comparison
Solution retrospective
This is my first challenge with JavaScript, I have never used JavaScript before in my life, I hope I did well and I hope you like it.
Community feedback
- @elaineleungPosted about 2 years ago
Hi Yaika Race, great job in completing this first JS challenge! Everything works well, and I like how you used some logic to handle what happens when nothing is selected. Great work also in displaying a warning reminder!
Two suggestions I have here:
-
Instead of using
<p>
, use an interactive element like<button>
instead, asp
is really not meant for this task (it's more of a pure text element), and buttons are more suited since they handle actions. Buttons also allow for attributes such asvalue
, and so you can store a value that you can later retrieve in the JS (e.g.,value="1"
). -
Great job using the
forEach
! I see that you also have theidx
in your function and that you later use the index for displaying the score. A simpler way that does not involve adding 1 to the index would be to just retrieve thetextContent
, or if you do use avalue
attribute in the button as I mentioned above, then you can useevent.target.value
or even just the element'svalue
. To see this in action, you can check out this mini CodePen I made for this challenge: https://codepen.io/elaineleung/pen/RwMqMxZ
Anyway, really great job on the whole in writing out all the JS here! 😊
Marked as helpful1 -
- @john-miragePosted about 2 years ago
Well done!
A personnal tip that you may find interesting (or not):
Selectors
- I always use different selectors for CSS and JS.
- I always use classes for CSS to have the same specificity. MDN - specificity
Classes for CSS
this example use the BEM methodology (recommended).
HTML
<ul class="list"> <li class="list__item"> <a class="list__link list__link--blue">home</a> </li> <li class="list__item"> <a class="list__link list__link--red">about</a> </li> </ul>
SCSS
.list { margin: 0; padding: 0; display: flex; flex-direction: row; align-items: center; &__item { padding-left: 1rem; padding-right: 1rem; } &__link { text-decoration: none; color: green; &--blue { color: blue; } &--red { color: red; } } }
data attributes for JS
Using id is great when you only have one element. But when you need to select multiple elements, you need to use a class wich is only used for CSS. Data attributes can be used instead of classes and ids to select elements with JS. more info here
Make sure that the data-id used for only one element is unique on the page (like an id);
HTML
<main class="app" data-id="app"></main> <ul class="list"> <li class="list__item" data-id="list-item"></li> <li class="list__item" data-id="list-item"></li> </ul>
JS
const appElement = document.querySelector('[data-id="app"]'); const listItemElements = document.querySelectorAll('[data-id="list-item"]');
Why it may be better:
- By using only classes for CSS, we avoid specificity issues.
- By looking at our code, we now know which elements are selected with javascript and wich are not.
- We can change a class without breaking a javascript selector. they are not related.
- We can change a data attribute without breaking CSS.
Marked as helpful1@YaikaRacePosted about 2 years ago@john-mirage Thanks for the tip! I didn't know about the BEM methodology nor about data attributes, I will investigate more about it and put it into practice!
0
Please log in to post a comment
Log in with GitHubJoin our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord