Hi how could write this code way more clean . It's a chaos 😀 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Hi how could write this code way more clean . It's a chaos 😀

I tried to use class to manipulate dom. But i dont like the way it is. I want it more clean and short maybe. https://code.sololearn.com/WmFzjjc1GIY1/?ref=app

26th Feb 2023, 6:04 AM
Javascript Developer
Javascript Developer - avatar
8 Answers
+ 5
the script should be in the 3rd tab(JS). maybe jquery is an option.
26th Feb 2023, 9:00 AM
Oma Falk
Oma Falk - avatar
+ 3
Zeinab you're right, there are shorter ways. What you did was technically correct, but it's the hard way to go about it. You don't have to define a class just to modify the DOM element. to get the same result, you could have just used <script> const mylist=document.getElementById("list"); mylist.children[0].textContent = "index 1"; mylist.children[1].textContent = "index 2"; </script>
26th Feb 2023, 5:02 PM
Bob_Li
Bob_Li - avatar
+ 1
Bob_Li no i meant to practice the use of class in manipulating dom. I was wondering if my way of using class has a cleaner way
26th Feb 2023, 7:24 PM
Javascript Developer
Javascript Developer - avatar
+ 1
1) separate javascript from html simply use window.onload = ()=>{ //your code goes here ...... } inside the javascript tab , the rest is okay to me . 2) practice however you want, happy coding :)
26th Feb 2023, 8:31 PM
Madrine Nansiimbi
Madrine Nansiimbi - avatar
+ 1
Zeinab using class is basically what frameworks like React and others work. It's just you're doing it from scratch. It will be a lot of work to recreate the functionalities, though. perhaps you could try exploring web components?
27th Feb 2023, 12:23 AM
Bob_Li
Bob_Li - avatar
+ 1
Bob_Li yeah i'm learning React
27th Feb 2023, 4:14 AM
Javascript Developer
Javascript Developer - avatar
0
Oma Falk i did'n get your mean
26th Feb 2023, 11:19 AM
Javascript Developer
Javascript Developer - avatar
0
Madrine Nansiimbi thanks sis🙏🏽
27th Feb 2023, 4:14 AM
Javascript Developer
Javascript Developer - avatar