Array of functions and loop

Hi, so i want to select a option from the list which will then call a function but its seems to not be working. Can you help? Thanks…

</><select id='moveSet'>
  
</select>
<button id='perform()'>Submit</button>
<p id='saySomething'></p></>

function fire() {
 document.getElementById('saySomething').innerText = 'hello';
};

function ice() {
  console.log('hi');
};


var videoSrcArr = new Array(fire, ice),
selectEl = document.getElementById('moveSet');

for(var i = 0; i < videoSrcArr.length; i++){
    selectEl.add(new Option(videoSrcArr[i].name, videoSrcArr[i].name));
};               

function perform() {
  let x = document.getElementById("moveSet").selectedIndex;
  if(x === 0) {
    videoSrcArr[0];
  } else if(x === 1) {
   videoSrcArr[1]; 
  }
};

Hi,

Thanks for sharing your code. Please remember to format with </> before pasting so that the code is readable.

You should check in your console and type in code to see where your code is not running, or alternatively set console logs to track what’s firing correcting.

Some things that can be preventing the code from running

  • selectEl is undefined (if it can’t find 'moveSet' id)
  • videoSrcArr is not a valid array.
  • as it is, you only define your perform function but you don’t yet invoke it.
1 Like

Yeah i typed the code into the console before i came here it said ‘cannot read property ‘add’ of null’, on line 14. I dont have a clue what that means…

As a test, in the console you can type document.getElementById(‘moveSet’); to see what it returns. It’s possible that it’s spelled differently. If it’s still undefined you’d have to check the html to verify that you’re spelling it consistently.

Also, you can always use the alternate method of document.createElement, create an option, and appendChild that option to the selectEl item.

1 Like

ive solved it, i forgot the () on the function call.

Nice! Outside of this code?

1 Like

Yeah outside this code. Sorry for the lat reply