Note that there are some explanatory texts on larger screens.

plurals
  1. POFunction Names as HTML Attribute
    primarykey
    data
    text
    <p>I know that functions can be stored on html attributes for specific events (e.g. onmouseover), but can any attribute refer to a function or just the special attributes?</p> <p>I would like to automatically wire the ajax success and failure call backs based on the form attributes [e.g. onsuccess="alert('whoohoo');" and onfail="alert('You suck');"]. Is something like that possible, or would I have to store the function on a known event and trigger the event on fail or success?</p> <hr> <p>I wanted to share the result code with SO for the benifit of the community. Unfortunally I could not use data-* as the attribute name due to '-' not being valid char for a property name and forms are created using MVC and anonymous types.</p> <pre><code>// provide the ability to manually setup a form for ajax processing. // target are the forms to be wired // success is the callback function function AjaxifyForms(target, success, fail) { $(target).each(function() { // Wierdness if each isn't used with validation $(this).validate({ submitHandler: function(form) { $.ajax({ //create an AJAX request type: "POST", //use POST (we could also load this from the form if wanted to) url: $(form).attr("action"), //get the URL from the form data: $(form).serialize(), datatype: "json", success: function(data) { if (success != undefined &amp;&amp; success != null) success(data); else if ($(form).attr("ajaxSuccssCallback") != undefined &amp;&amp; $(form).attr("ajaxSuccssCallback") != null) window[$(form).attr("ajaxSuccssCallback")](data); else alert("Success"); }, error: function(XMLHttpRequest, textStatus, errorThrown) { if (faile != undefined &amp;&amp; fail != null) fail(data); else if ($(form).attr("ajaxFailCallback") != undefined &amp;&amp; $(form).attr("ajaxFailCallback") != null) window[$(form).attr("ajaxFailCallback")](data); else alert("Epic Fail"); } }); return false; //don't forget to cancel the event so we don't submit the page! } }); }); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload