JavaScript | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

JavaScript

Guys, I want to write a code to accept a number(x) in a text box and display an image according to the sum i enter. Eg. If i enter 5 and i click the button(call the function) i want to see 5 images of Fanta bottles. Any Help? Thanks

31st Jan 2018, 5:29 PM
Nana Boafo Yeboah
Nana Boafo Yeboah - avatar
4 Answers
+ 3
<button onclick=' var no=prompt("Enter a no"); for(var i=0;i<no;i++) {$("body").append("<img src='link.png'>"); } ' > </button> Using jquery
31st Jan 2018, 5:38 PM
᠌᠌Brains[Abidemi]
᠌᠌Brains[Abidemi] - avatar
+ 1
u can keep ur image inside a loop and the input value have to pass in loop var x = document. getElementById (' ').val () for (y=0;y>=x;y++){}
31st Jan 2018, 5:41 PM
fahim
+ 1
JavaScript without anything <button id="myID">Click</button> <script> document.getElementById("myID").addEventListener("click",function() { for(let i = parseInt(prompt("Number please"), 10)-1; i >= 0; i-=1){ let image = document.createElement("img"); image.src = "fanta.jpg"; document.body.appendChild(image); } }); </script>
31st Jan 2018, 5:44 PM
Draphar
+ 1
Thanks Guys. I appreciate the help.. I just need the JavaScript code to achieve my results this way <!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <input type=“text” id=“nNum”> <button onclick=“run_()”>Run</button> </body> </html>
31st Jan 2018, 6:38 PM
Nana Boafo Yeboah
Nana Boafo Yeboah - avatar