HTML and Javascript - How do we make an onclick button for a random list item? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 11

HTML and Javascript - How do we make an onclick button for a random list item?

I'd like to make a random button like this: https://code.sololearn.com/WpDt913j6V8m/#html The only difference is that I'd like it to display a random HTML list item instead of a random string in the array. What I have down so far: https://code.sololearn.com/Ws7tzq1v95TY/#html I think it's necessary to associate the collection of <li> with items in a JavaScript array. I've struggled much on aggregating this concept and that of random selection into code.

29th Sep 2020, 8:16 AM
Solus
Solus - avatar
6 Answers
+ 6
something like this? you can use object. https://code.sololearn.com/W51GwZQtHzyr/?ref=app
29th Sep 2020, 8:26 AM
Raj Chhatrala
Raj Chhatrala - avatar
+ 5
I edited the code to show you how you can do that. If you want to add more items like performed by and other then add more properties to object in array.
29th Sep 2020, 8:48 AM
Raj Chhatrala
Raj Chhatrala - avatar
+ 5
RDC WOW. Your code is so organized... You're a real master. That's a level 17 for you haha. There's just one problem. It seems like most of the things now have migrated over to Javascript. As for CSS, how are we supposed to apply styling techniques to the texts and headings if none exist in HTML?
29th Sep 2020, 9:09 AM
Solus
Solus - avatar
+ 5
You don't have to worry about that. Just write your css and when you dynamically add tag to your HTML it will be applied. I again edited the code and added css demo. P.s. level 17 does not have to do anything with skills. :)
29th Sep 2020, 9:12 AM
Raj Chhatrala
Raj Chhatrala - avatar
+ 5
Got it. Thank you for ending my month-long struggle. P.s. It must have something to do with your name then... RDC = Real-Deal Coder >=]
29th Sep 2020, 9:23 AM
Solus
Solus - avatar
+ 2
RDC How will your JS portion allow us to include various nested tags within the list item? For instance, the <iframe> and <a href> inside the list items? Reference: https://code.sololearn.com/Ws7tzq1v95TY/#html
29th Sep 2020, 8:41 AM
Solus
Solus - avatar