document.writeln()

Joined
Aug 8, 2017
Messages
6
Likes
0
Best answers
0
#1
If in javascript I used document.writeln() to make an order list, then used prompt() for the user to input their answer (in number) and finally using writeln() to output the answer with text (eg 'Your favourite food is ', i ask how can I output the name from the list (in this example fav. food) instead of the list number it was associated with?
 
Last edited:

TopSilver

web developer
Administrator
Joined
Mar 10, 2017
Messages
326
Likes
77
Best answers
0
#2
@FamousDEXTER! You can also use real answers with document.writeIn(). It doesn't have to be an ordered list of numbered foods. You can then use prompt() to ask for their favorite food and they can pick their favorite food and enter it in the prompt box without having to use a number.

document.writeIn() is the same as document.write() except it adds a new line. Anyways I heard you fixed it via discord and found out it was a missing semicolon. So that's good. Anyways glad I could help.
 
Likes: FamousDEXTER!

SeeSharp

New Member
Joined
Jan 2, 2018
Messages
11
Likes
9
Best answers
0
#4
something like this?

HTML:
<!DOCTYPE html>
<html>
<body>
<script>
var foodArray = new Array();
foodArray[0] = "Please select a number for your favourite food<br><br>";
foodArray[1] = "beef";
foodArray[2] = "fish";
foodArray[3] = "chicken";
document.write(foodArray[0]);
document.write('1. ' + foodArray[1] + '<br>');
document.write('2. ' + foodArray[2] + '<br>');
document.write('3. ' + foodArray[3] + '<br>');
</script>
<br>
<input type="text" id="inputboxid" placeholder="enter number" />
<button onclick="getChoice()">Submit choice</button>
<div id='result'></div>
<script>
    function getChoice()
    {
    var inputboxid = document.getElementById("inputboxid").value;
        if (/^\s*$/.test(inputboxid)) {
        }
        else {
                result.innerHTML = '';
                result.innerHTML += '<br>Your favourite food is ' + foodArray[inputboxid];
             }
    }
</script>
</body>
</html>
 
Likes: TopSilver