0

I need some help for my website. I'm trying to iterate with an array and two different input field. my html file contains like below:

<input name="comune" type="text" id="comune" list="comuni" />
<datalist id="comuni">
</datalist>
<input name="provincia" type="text" id="provincia" />

Let's say that my array is like this:


    var comProv = {
    'Pisa' : 'PI',
    'Firenze' = 'FI',
    'Roma' = 'RM'}

I would like to populate the datalist with the Key of the array and, on selection of specific option, I would like to show in the second input field the value of the same array?s item.

I tried with this code


    $('#comune').keyup(function(){
        var options = '';
        for(var i = 0; i < comProv.length; i++){
            options += '<option value="'+comProv[i]+'" />';
        }
        document.getElementById('comuni').innerHTML = options;
    });

I stopped without moving to the second topic cause I'm stack. Anyone can help? Thank you in advance.

4
  • 1
    use for...in loop Commented Apr 9, 2020 at 19:36
  • ok I will try now Commented Apr 9, 2020 at 19:38
  • 2
    That's an object, not an array. Commented Apr 9, 2020 at 19:39
  • 1
    I'm totally new in programming (I'm not a programmer but a lawyer) that's why for me is not so easy Commented Apr 9, 2020 at 19:42

2 Answers 2

1

It's an object, not an array. You can iterate it using $.each().

$('#comune').keyup(function(){
    var options = '';
    $.each(comProv, function(key, value) {
        options += `<option value="${value}">${key}</option>`;
    });
    $('#comuni').html(options);
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank to you also very much
0

First of all, it is not an array, it is an object. Second, you need to use "for...in" loop to loop object or $.each if you are using jQuery library. Last but not least, you have typo error in your object. Finally, try this code that is in this snippet:

$(function(){

    var comProv = {
        'Pisa' : 'PI',
        'Firenze':'FI',
        'Roma': 'RM'
    }

    $('#comune').keyup(function(){
        var options = '';
        $.each(comProv, function(key, value) {
            options += `<option value="${value}">${key}</option>`;
        });
        $('#comuni').html(options);
    });


})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input name="comune" type="text" id="comune" list="comuni" />
<datalist id="comuni">
</datalist>
<input name="provincia" type="text" id="provincia" />

10 Comments

Thank you very much. About the second question...I need to put the key in the on keyup in the first input and the value in the second input field. Is it possible? then I will disappear :)
so in first input it should be Pisa for example and in the second Input P1?
but what if they will again put the cursor on the second input?))) you value will change then, it is no good
I'm evaluating or to substitute with a label or anyway I want to make the second input "readonly"
use $('#inputId').prop('readonly', true); to make it readonly)
|

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.