AddEventListener works in firefox but not chrome
This code works in Mozilla Firefox 31.0, but not in Google Chrome 38.0.2125.104 How to solve this problem?
Please note: Using change () or jQuery is not a parameter here!
<body>
<select id="category">
<option value="movies">Movies</option>
<option value="artist">Artists</option>
<option value="authors">Authors</option>
<option value="chemistry">Chemistry</option>
</select>
</body>
<script>
window.onload = initialise;
function initialise() {
var options = document.getElementsByTagName('option');
for (var i = 0; i < options.length; i++) {
options[i].addEventListener("click", getQuestion, false);
//console.log(i);
}
}
function getQuestion() {
console.log("getting question..");
}
</script>
</html>
+3
source to share
2 answers
I don't know why they are all optional, but aren't you looking for the on change function? So you can just get attached to it one day?
//Untested
var Category = document.getElementsById('category');
Category.addEventListener("change", getQuestion, false);
Also, if you don't mind jQuery you may need to learn it, it often gets rid of cross-browser issues. In JQuery:
$('body').on('change', '#category', function() {
getQuestion($(this));
})
+1
source to share