0

I am trying to number each result so that it says:

City #1 is Los Angeles. City #2 is San Francisco. City #3 is... and so on.

I was thinking that I had to write something inside the for loop?

I am working with Notepad++.

<html>
 
<body style="text-align: center;">
 
    <h2 id="hi">Enter Your Five Favorite Cities</h2>
    <form class="" action="index.html">
        <input type="text" name="favoriteCities[]" value="" /><br>
        <br>
        <input type="text" name="favoriteCities[]" value="" /><br>
        <br>
        <input type="text" name="favoriteCities[]" value="" /><br>
        <br>
        <input type="text" name="favoriteCities[]" value="" /><br>
        <br>
        <input type="text" name="favoriteCities[]" value="" /><br>
    <br>
        <button type="button" name="button" onclick="favoriteCities()">Submit</button>
   
    </form>
 
    <h3 id="hi">Results</h3>
 
    <p id="output"></p>
    
    <script type="text/javascript">
        var r = "";
        function favoriteCities() {
            var input = document.getElementsByName('favoriteCities[]');
 
            for (var i = 0; i < input.length; i++) {
                var a = input[i];
                r = r + "City #" + "???" + "is " + a.value + ("<br>");
            }
            document.getElementById("output").innerHTML = r;
        }
    </script>
    
    
</body>
 
</html>
1
  • 1
    The i index you use in the loop is an unique number, so r = r + "City " + i + " is " + a.value + ("<br>"); or something? Commented Sep 1, 2022 at 15:25

1 Answer 1

1

As quoted, you can use the index of the loop to know the city count.

var r = "";
function favoriteCities() {
    var input = document.getElementsByName('favoriteCities[]');

    for (var i = 0; i < input.length; i++) {
        var a = input[i];
        r = r + "City #" + ( i + 1 ) + " is " + a.value + ("<br>");
    }
    document.getElementById("output").innerHTML = r;
}
    <h2 id="hi">Enter Your Five Favorite Cities</h2>
    <form class="" action="index.html">
        <input type="text" name="favoriteCities[]" value="" /><br>
        <br>
        <input type="text" name="favoriteCities[]" value="" /><br>
        <br>
        <input type="text" name="favoriteCities[]" value="" /><br>
        <br>
        <input type="text" name="favoriteCities[]" value="" /><br>
        <br>
        <input type="text" name="favoriteCities[]" value="" /><br>
    <br>
        <button type="button" name="button" onclick="favoriteCities()">Submit</button>
   
    </form>
 
    <h3 id="hi">Results</h3>
 
    <p id="output"></p>

Sign up to request clarification or add additional context in comments.

2 Comments

Might be worth looking at template strings to remove all that concatenation.
Absolutely true, but in my very humble opinion, I doubt it's within the OPs current level of JS experience. So including templates would only confuse them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.