User is adding a new search field (input field + select element). When user selects from select element I am trying to update the "name" value from the input element. Unfortunately the following code results in the following error at the console.log line
TypeError: null is not an object (evaluating 'document.getElementById(selectID).options')
here is the code:
function updateName2(index){
var selectID = "SEL"+index;
var inputID = "AS"+index;
var e = document.getElementById(selectID);
var valueSelect = e.options[e.selectedIndex].text;
console.log(valueSelect); //error here
document.getElementById(inputID).name = valueSelect;
};
var index = 2;
// ADD NEW SEARCH FIELD
$("#addNewSearchField").click(function() {
$("#Form").append(
"<input id='AS"+index+"' type='text' name='' value=''>"+
"<select id='SEL"+index+"' onChange='updateName2(index)'>"+
"<?php
foreach ($fields as $key => $value) {
if($key=="select"){
echo "<option selected value='".$key."'>".$value."</option>";
}else{
echo "<option value='".$key."'>".$value."</option>";}
};
?>"
+"</select><br>"
);
index += 1;
})
console.log(e)it should benullwitch explains the rest.TypeError: null is not an object (evaluating 'e.options')so it does not find the object at all ?updateName2(index)should beupdateName2('+index+')and I agree but I dont think that is the root of your issue.var valueSelect...anddocument.getElement...then console.log(e) what do you get?