0

I have a form and user will select city. When the user selects the city, the town list will change to bring it to the cities of the towns.

I want to delete the towns of non-selected cities.

I have a code but it does not work :

       <div class="form-group">
            <label for="city">City :</label>
            <select name="city" id="city" class="form-control" required onchange="townChange()">
                @foreach (var cityItem in Model.cityList)
                {
                    <option value="@cityItem.Id">@cityItem.Name</option>
                }
            </select>
        </div>
        <div class="form-group">
            <label for="town">Town :</label>
            <select name="town" id="town" class="form-control" required>
                @foreach (var townItem in Model.townList)
                {
                    <option value="@townItem.Id" accesskey="@townItem.CityId">@townItem.Name</option>
                }
            </select>
        </div>

function townChange()
{
    var cityId = document.getElementById("city").value;
    var townlist = document.getElementById("town");
    for (var i = 0; i < townlist.length; i++) {
        if (townlist.options[i].accessKey == cityId)
            townlist.remove(i);
    }
}
3
  • 2
    You should not try to loop townlist-variable since it only contains the town select-element. Try loopin through the townlist.options instead. Commented Jun 22, 2017 at 12:35
  • But this is the example of deletion w3schools.com/jsref/met_select_remove.asp. @Esko Commented Jun 22, 2017 at 12:43
  • Possible duplicate of HTML select dropdownlist with javascript function Commented Jun 22, 2017 at 12:54

3 Answers 3

0

You tagged jquery but you are not actually using jquery in your code.

If you are willing to try jquery, something like this should work, but I am unable to test at the moment:

$('#town option').each(function () {
  if ($(this).prop('accesskey') !== cityId) {
    $(this).remove();
  }
});

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

Comments

0

document.getElementById("town") you were looping with this which is the element itself u need to loop through the options of the select element. Try this function

function townChange()
{
    var cityId = document.getElementById("city").value;
    var townlist = document.getElementById("town");
    var options = townlist.options;
    for (var i = 0; i < options.length; i++) {
        if (townlist.options[i].accessKey != cityId)
        {
            townlist.remove(i);
        }
    }
}

6 Comments

This method works a bit, but does not delete all of the things that need to be deleted. @Riaz
@D.Dirik i deleted all options which dosent matches the townlist.accessKey with the selected cityId . is the logic correct ?
since u said "I want to delete the towns of non-selected cities" ? so i did that , i gusess the logic is correct.
The logic is correct but unfortunately it does not work exactly. @Riaz-laskar
@D.Dirik ok so whats the issue can u elaborate a little plz
|
0

Replace your loop with this.

for (var i = 0; i < townlist.length; i++) { if (townlist.options[i].accesskey == cityId) townlist.remove(i); }

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.