Html Select List: why would onchange be called twice?

Posted by Mark Redman on Stack Overflow See other posts from Stack Overflow or by Mark Redman
Published on 2010-05-22T16:10:31Z Indexed on 2010/05/23 13:51 UTC
Read the original article Hit count: 741

Filed under:
|
|

I have a page with a select list (ASP.NET MVC Page)

The select list and onchange event specified like this:

<%=Html.DropDownList("CompanyID", Model.CompanySelectList, "(select company)", new { @class = "data-entry-field", @onchange = "companySelectListChanged()" })%>

The companySelectListChanged function is getting called twice?

I am using the nifty code in this question to get the caller.

both times the caller is the onchange event, however if i look at the callers caller using:

arguments.callee.caller.caller

the first call returns some system code as the caller (i presume) and the second call returns undefined.

I am checking for undefined to only react once to onchange, but this doesnt seem ideal, whcy would onchange be called twice?

UPDATE:

ok, found the culprit! ...apart from me :-) but the issue of calling the companySelectListChanged function twice still stands.

The onchange event is set directly on the select as mentioned. This calls the companySelectListChanged function.

..notice the 'data-entry-field' class, now in a separate linked javascript file a change event on all fields with this class is bound to a function that changes the colour of the save button. This means there is two events on the onchange, but the companySelectListChanged is called twice?

The additional binding is set as follows:

$('.data-entry-field').bind('keypress keyup change', function (e) { highLightSaveButtons(); });

Assuming its possible to have 2 change events on the select list, its would assume that setting keypress & keyup events may be breaking something?

Any ideas?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about html