I have a js file:
I am trying to read the value f a dropdown list which has an ID = "topn". When I do that at POS1 it gives an error :
Cannot read property 'options' of null
At POS2 it works fine. But I want the value at POS1. Where have I gone wrong??. I am using latest version of Google Chrome.
window.onload = getAll();
function getAll() {
var data = new FormData();
//console.log(document.location.href);
/* POS1 */
var test = document.getElementById("topn");
console.log(test.options[test.selectedIndex].value);
/**/
data.append('top_n', test.options[test.selectedIndex].value);
data.append('top_n', 5);
var xhr = new XMLHttpRequest();
xhr.open('POST', 'http://xyz.php', true);
xhr.onload = function() {
console.log("done here");
var table_dom = document.getElementById("tab1");
/* POS2 */
var test = document.getElementById("topn");
console.log(test.options[test.selectedIndex].value);
/**/
response_text = this.responseText;
response_text.replace(/['"]+/g, '');
console.log(response_text);
table_dom.innerHTML = response_text;
};
xhr.send(data);
}