-2

I've got a page im trying to develop which will take user input and then add it to an array and then take that array and make a select and option list out of it as such.

<!DOCTYPE>
<html>
<head>
<script>
 var option_list = [];
   // for loop at thats option_list to select select tag
 for (var i = 0; i < option_list.length; i++){
  var opt = option_list[i];
  var option = document.createElement("option");
  var select = document.getElementById("select");     
  option.setAttribute("id", opt); //Adding ID to the option list
  option.setAttribute("class", "intersect-option"); // adds classes to option
  option.value(opt); // adds value to option list 
  select.appendChild(option); // Adds option to the list.

} 
function add_option(name){
var name = document.getElementById("name").value;
name.push(option_list());
}
</script>
</head>
<body>
 <input type="text" id="name" value="">
 <input type="button" onclick="add_option()" value="add person">
 <select id="select">
</select>

</body>
</html>

The issue i am having is that it says when i try and input the information i get back-

Uncaught TypeError: option_list is not a function

at add_option (selectTest.html:19)

at HTMLInputElement.onclick (selectTest.html:25)

I am unsure what i am doing wrong and any help would be greatly appreciated.

2
  • You are trying to call option_list as a function in the last line of your script (name.push(option_list());). The correct syntax would be option_list.push(name). Commented Mar 15, 2018 at 13:14
  • 1
    Possible duplicate of storing user input in array Commented Mar 15, 2018 at 14:12

4 Answers 4

0

You should push name into option_list array. The correct syntax is option_list.push(name);

function add_option(){
    var name = document.getElementById("name").value;
    option_list.push(name);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Pushing elements into array works the other way:

option_list.push(name);

http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push

Comments

0
name.push(option_list());

As it says, option_list is not a function, and balanced parentheses after an identifier designate a function call in C family languages, so option_list() is not a valid expression.

Additionally, did you really intend to loop from zero up to the length of an empty array (which is equally zero)?

Comments

0
    <!DOCTYPE>
    <html>
    <head>

    </head>
    <body>
     <input type="text" id="name" value="">
     <input type="button" onclick="add_option()" value="add person">
     <select id="select">
    </select>
    <script>
     var option_list = [];
       // for loop at thats option_list to select select tag

     
      var select = document.getElementById("select");     

    function add_option(name){
    var name = document.getElementById("name").value;
     var option = document.createElement("option");
      var label = document.createTextNode(name);
      option.setAttribute("id", name); //Adding ID to the option list
      option.setAttribute("class", "intersect-option"); // adds classes to option
      option.value = name; // adds value to option list 
      
      option.appendChild(label);
      select.appendChild(option); // Adds option to the list.
    }



    </script>
    </body>
    </html>

1 Comment

If you find it useful.Please tick it as answer.

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.