Note that there are some explanatory texts on larger screens.

plurals
  1. POJQuery - replace handler for live event
    text
    copied!<p>My original question I've managed to answer by cobbling other answers together from this site (thank you StackOverflow!) - just posting here to help others wondering.</p> <p>I have a live event like:</p> <pre><code>function addValidator(selector, validator) { // Add the selector and validator to a hash map $(selector).live('change', validateTag); } </code></pre> <p>The table rows are going to vary in number at any time and I don't want to tightly couple the table creation code with the validation code.</p> <p>Later another validation function comes along not knowing anything about the first, and I need to avoid that same live selector firing the validateTag function twice, which is going to aggregate all validation messages and present a single UI about a given input.</p> <p>So - each time someone calls addValidator I need to actually REPLACE the live event handler, or more specifically, expand on its selector (since I'm still calling validateTag, I just need the live handler to cover more ground). I'll post the answer I came up with below, happy to hear more.</p> <p>-- Update to clarify what I'm doing</p> <p>I have several pages with dynamic tables that could include any number of rows. I don't want the validation and the table management code to be tightly coupled. I want to be able to set out inputs in the table rows like:</p> <pre><code>&lt;input class="required email"/&gt; </code></pre> <p>And have validation validate it for both the required and email rules onchange(). In code:</p> <pre><code>Valid.addValidator('input.required', Valid.required); Valid.addValidator('input.email', Valid.email); </code></pre> <p>Obviously as rows are added and removed I could keep reapplying the events to the inputs, but that tightly couples the table builder code and the validation code.</p> <p>Another hack is to just build a single live event like:</p> <pre><code>$('input, select, textarea').live('change', Valid.validateTag); </code></pre> <p>And then use a hashmap to look up what exactly applies to this specific tag, but that assumes the only things I'll ever validate are inputs, selects and textareas, and that the only kind of validation I'll ever want to do is onchange.</p> <p>What I really want is to let any kind of validation rule match any tags by selector, without either of the above. The second hack isn't too bad, but it is a false assumption and is inflexible to custom controls that don't use one of those 3 basic tags.</p>
 

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