Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript reserved word: "preset"

Tags:

javascript

I have a form with a select dropdown, and my select tag looks like this:

<select name='preset' onchange='preset(this);'>

Right now I have my JavaScript function just do alert('test');. Well, when I change my selection in the dropdown, I'm getting an error saying "preset is not a function". Yes, I verified that it's spelled right, and I even did a generic call to it on page load and got my alert.

If I change my function name to something else, like presetx it works just fine. So I thought maybe "preset" was some kind of reserved word in JavaScript, but I can't seem to find anything saying as such. Why would this happen?

Update

Currently I don't have anything else on my test page except for my form and the function. No framework includes or other code, so I know it's not anything like that.

like image 684
newbiejsperson Avatar asked Jul 03 '11 16:07

newbiejsperson


1 Answers

Some browsers map elements with name attributes to global variables. So <select name='preset' onchange='preset(this);'> actually creates (in some browsers) a global property preset. This overwrites the preset function.

Since preset is now an HTMLSelectElement object, not a function, you get a "not a function" error.

like image 67
lonesomeday Avatar answered Sep 22 '22 07:09

lonesomeday