0

I want to regenerate List2 (selSortt) everytime the user changes the selection in List1 (selQueryType). In its original state, List2 is blank. Both lists are part of a larger HTML input document. List1 has 4 options, so in the end there will be 4 ifs - actually a switch. List 2 is generated from a predefined array (listvalues), NOT from List1 (see code).

Function QueryTypeChg() is fired by onchange List1 - I checked with an alert, it is fired. The problem seems to be in the for cycle, when trying to generate the new options of List2. No options are added to List2 - it remains blank, as it is originally.

function QueryTypeChg()
{
var selIndex = document.getElementById("selQueryType").selectedIndex;

var objSel = document.getElementById("selSortt");
var i = 0;

while (objSel.length > 0)
   {
   objSel.remove(0);
   }

if (selIndex == 0)
   {
   var listvalues = ["Species", "Region, area", "Anthesis", "Vulnerability"];

   for (i = 0; i < options.length; i++) 
       {
       var option = document.createElement("option");
       option.text = listvalues[i];
       option.value = i;       
//       alert("option.text is " + option.text);
       objSel.add(option);
       }​
   }


}

3 Answers 3

1

because you are removing the element by doing

while (objSel.length > 0)
{
  objSel.remove(0);
}

If you want to clear the select then simply replace this while loop with

objSel.innerHTML = "";

Complete code should be

function QueryTypeChg()
{
  document.getElementById("selSortt").innerHTML = document.getElementById("selQueryType").innerHTML 
  document.getElementById("selSortt").selectedIndex = document.getElementById("selQueryType").selectedIndex;
}
Sign up to request clarification or add additional context in comments.

9 Comments

@RayonDabre remove is removing that element from its DOM Tree, please check the document link attached
OP wants to remove all the options and then add new options to the select.. What is wrong with the code he has written ? innerHTML = "" could be another approach but why do we need it ?
I assumed that by doing remove(0) he intended to remove the child but instead he ended up removing the element itself. I have posted another much simpler approach.
The remove() method is taken from W3Schools, which says it is the opposite of add() - adding an option to a list. w3schools.com/jsref/met_select_remove.asp. As I clearly stated, I just want to re-generate the list, not to remove it from the document. But this is secondary; in the original state, the list is EMPTY, and my code DOES NOT add new options to the list from array listvalues. This is for now my problem. :)
@Mikey Check the link I have shared, it removes the current node from DOM tree. Did you tried the code I have shared?
|
0

You have use options variable instead of listvalues please change below code which might help you

 for (i = 0; i < options.length; i++) 

with

for (i = 0; i < listvalues.length; i++) 

1 Comment

True, but this is not the problem. Thanks.
0

I "nailed" it with the following code:

function QueryTypeChg()
{
var selIndex = document.getElementById("listQueryType").selectedIndex;
localStorage.setItem("QueryType", selIndex);

if (selIndex == 0)
   {
   var listvalues = ["Species", "Region, area", "Anthesis", "Vulnerability"];
   var str_options = '';

   for (var i=0; i < listvalues.length; i++)
       {
       str_options += "<option value='" + [i] + "'>" + listvalues[i] + "</option> \r\n";
       }

   $("#listSort").html("");
   $("#listSort").append(str_options);
   return;
   }

}

I still don't know why it didn't work with the initial code. It DOES work in the W3Schools Try It window and in other examples I've seen. What's also peculiar is that it uses the html() method, not the innerHTML property. What kind of all-over-the-place language is this JavaScript anyway? In Pascal there is always ONLY ONE WAY to do a certain thing!

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.