1

I have an array Orte that contains postcode and the name of the city. Now I want to change the dropdown depending on the number entered in the input field. The input field has an onchange function. The function is the one below.

The script works so far except the last line after the for loop. It never adds the last part and I don't know why. Can someone help me please.

Thanks in advance for the anwsers! Burzi

function updateOrt(eingabe){    
    document.getElementById("ort_platzhalter").innerHTML = '<select name="ort">'
    for (var i = 1; i <= Orte.length; i++){ 
        if(Orte[i].PLZ == eingabe){
            document.getElementById("ort_platzhalter").innerHTML += '<option value="' + Orte[i].id + '">' + Orte[i].Ort + '</option>'
        }
    } 
    document.getElementById("ort_platzhalter").innerHTML += "</select>"
}
5
  • What is the "last part"? Commented Dec 26, 2014 at 15:34
  • 1
    use var myOrt = document.getElementById("ort_platzhalter"); don't retrieve it again every time . Commented Dec 26, 2014 at 15:34
  • Arrays are zero-based. i=0; i<Orte.length Commented Dec 26, 2014 at 15:36
  • 1
    Using .innerHTML += htmlPart; will most likely never have the expected result. Whenever you change innerHTML it will immediately parsed into DOM, as of that an incomplete html code will be fixed. innerHTML += "</select>" will never have an effect, as .innerHTML cannot be incomplete. Commented Dec 26, 2014 at 15:36
  • I'm also german, but please don't write names/functions/vars in german. It's a nightmare for every non-german developer you want to work with or who needs to work with your code. Write international code. Commented Dec 27, 2014 at 20:16

2 Answers 2

1

You should update the innerHTML in one shot. Build up the string first, then assign it. Also, arrays are zero-indexed, so I am guessing you want to start at 0, and end once it is the length of Orte (i = Orte.length).

function updateOrt(eingabe){
    var str = '<select name="ort">'
    for (var i = 0; i < Orte.length; i++){ 
        if(Orte[i].PLZ == eingabe){
            str += '<option value="' + Orte[i].id + '">' + Orte[i].Ort + '</option>'
        }
    } 
    str += "</select>"
    // now assign the str to innerHTML
    document.getElementById("ort_platzhalter").innerHTML = str;
}
Sign up to request clarification or add additional context in comments.

Comments

0

your problem is in the array loop.

You are going from 1 to Orte.length, when it reaches Orte.length it throws an exception and the code after the loop does not execute.

you should do it this way:

function updateOrt(eingabe){    
    document.getElementById("ort_platzhalter").innerHTML = '<select name="ort">'
    for (var i = 0; i < Orte.length; i++){ 
        if(Orte[i].PLZ == eingabe){
            document.getElementById("ort_platzhalter").innerHTML += '<option value="' + Orte[i].id + '">' + Orte[i].Ort + '</option>'
        }
    } 
    document.getElementById("ort_platzhalter").innerHTML += "</select>"
}

because arrays in javascript are 0 indexed and go up to length-1

Comments

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.