views:

323

answers:

1

Having some pain with this event, I have some code like this for a dropdown:

return helper.DropDownList(data.ModelEntityId.ToString(), selectList, "<Select>", new { onChange = onChange });

where onChange is a variable containing a javascript function to run, this works great. However with the textbox version (below) the onchange event is fired when the page loads (unlike the DropdownList, different behavior is annoying). When the javascript function is called at the time the page is loading I get the error: Javascript "undefined" could not be found... (it then works subsequently)

return helper.TextBox(data.ModelEntity.ModelEntityId.ToString(), data.ValueText, new { onchange = onChange });

Example javascript

function SuitabilityChecked(providerId, checkId, parentId) {
            alert("meep"); // just to test error still occurs
        };

Is there a different event I should use for a textbox, or something "special I need to do?

+2  A: 

You could avoid this problem entirely by applying your handlers unobtrusively (not inline) after the page load is complete. The standard way to do this in MVC is with jQuery.

 <script type="text/javascript">
    $(function() {
         $('input#<%= data.ModelEntity.ModelEntityId %>').change( function() {
              ...
         });
    });
 </script>

Using classes would make it even easier to apply the handlers to only certain elements, if desired -- just add the class definition to the htmlAttributes hash in the helper and change the selector in the onloaded handler to apply the change handler to only those elements with the selected class.

tvanfosson
How would this work considering that my inline statement are dynamic? (the onChange variable is loaded from the database)I guess my jquery would have to loop through my onChange table and apply the line above...
Grayson Mitchell
Or you could simply generate a single javascript function that uses a switch statement to choose the correct handler to invoke (perhaps built using a specialized helper) and apply the same handler to all.
tvanfosson
OK, trying to get the fundamentals working, I cant get the change event to fire (I get the meep alert, but no meep 2)... jQuery(document).ready(function() { alert("meep"); $('input#67').change(function() { alert("meep 2"); }); });
Grayson Mitchell
Ok -- I didn't know the ids were numeric. Ids in HTML have be both unique and start with a letter. It won't work if they are just numeric.
tvanfosson
lol, my whole site is based on a numeric id structure (is the primary key value in the db). Everything else works with numerics (I can do this: "var value1 = $("#" + id).val();" for instance)
Grayson Mitchell
just tried changing the ids to lead with an alpha... still does not work, so it might be another issue
Grayson Mitchell
ahh, got rid of the 'input' at the front and it worked!
Grayson Mitchell
Yes -- the `input` would only select inputs, not selects. I'm surprised that numeric ids work. Are you sure they do in all browsers? They are definitely non-conforming: http://www.w3.org/TR/html4/types.html#type-name "ID and NAME tokens must begin with a letter ([A-Za-z]) ..."
tvanfosson