javascript - Pure JS: Listen for onchange event? - Stack Overflow

This is giving me "4: Uncaught SyntaxError: Unexpected token ILLEGAL":(function() {var select

This is giving me "4: Uncaught SyntaxError: Unexpected token ILLEGAL":

(function() {
    var select = document.getElementById('select-card');

    ​select.onchange = function() { };
})();

What should I be doing differently?

This is giving me "4: Uncaught SyntaxError: Unexpected token ILLEGAL":

(function() {
    var select = document.getElementById('select-card');

    ​select.onchange = function() { };
})();

What should I be doing differently?

Share Improve this question asked Feb 19, 2015 at 17:28 RichardRichard 65.7k135 gold badges356 silver badges571 bronze badges 2
  • Maybe the select keyword is bugging. It's on the window object. – Mouser Commented Feb 19, 2015 at 17:30
  • 2 Actually that's not it, you have a non-printing character in front of select. See a red dot at jsFiddle. – Teemu Commented Feb 19, 2015 at 17:36
Add a ment  | 

3 Answers 3

Reset to default 6

I expect that you have an element with id select-card. Your code is otherwise fine.

But you should be using.

select.addEventListener("change", function(){  } );

Edit the code to this: select.onchange = (function() { }); Whenever you're doing a function expression and calling it immediately, you need those parens because there's a parsing ambiguity otherwise.

I couldn't wrap my head around this. The code is correct however is was bugging still. Found it, it appeared when I converted my test page from UTF8 to ANSI:

There is a incorrect return/character in your code. That is causing the bug.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744387024a4571715.html

相关推荐

  • javascript - Pure JS: Listen for onchange event? - Stack Overflow

    This is giving me "4: Uncaught SyntaxError: Unexpected token ILLEGAL":(function() {var select

    7天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信