conditionize.js icon indicating copy to clipboard operation
conditionize.js copied to clipboard

Add triggering of events on show and hide

Open landwire opened this issue 6 years ago • 6 comments

Hi there again, I find it useful if some events would be triggered on the element on hiding and on showing. I am dealing with some validation and required fields (inside hidden sections) on the FE and BE side. So I need to toggle the required attribute depending if the section is shown or not.

I do not know how to do a PR, but this is basically what I would want/need:

$.fn.showOrHide = function(is_met, $section) {
        console.log('is_met', is_met);
      if (is_met) {
        $section.slideDown();
        $section.trigger('sectionvisible');
      }
      else {
        $section.slideUp();
        $section.find('select, input').each(function(){
            if ( ($(this).attr('type')==='radio') || ($(this).attr('type')==='checkbox') ) {
                $(this).prop('checked', false).trigger('change');
            }
            else{
                $(this).val('').trigger('change');
            }
        });
        $section.trigger('sectionhidden');
      }
    };

landwire avatar Nov 29 '18 15:11 landwire

I would love this ability too. In the past I've had to use jQuery to toggle requirements based on visability but if this lib could do it that would be awesome +1

dcblogdev avatar Nov 29 '18 15:11 dcblogdev

Hi, Nice suggestion! I really like the idea. Actually, I would generalize it. I think it would be better to make it something like that:

.conditionize({
    'hideJS': false,     // default true
    'showHide': true,    // default true
    'clearOnHide': true, // default true
    'toggleClass': ['required-field', 'is_visible'], // default undefined
    'customHandler': function(is_met, $section) {
        if (is_met) {
            alert('The condition is met');
        }
    }
  });

That would work for your case too, right?

P.S. I am not sure that list of parameters should be exactly as in my example. I just wanted to show the idea.

rguliev avatar Dec 01 '18 13:12 rguliev

Yeah, I guess. Thanks for the suggestion. Would be nice to enhance this little script a bit like this.

I was also thinking if eval() could be replaced. Not sure if there would be any security issues in this case. Where I use the script a person can just create the conditions themselves. Not sure what would happen if they hacked in some malicious code...

landwire avatar Dec 03 '18 12:12 landwire

I was also thinking if eval() could be replaced. Not sure if there would be any security issues in this case. Where I use the script a person can just create the conditions themselves. Not sure what would happen if they hacked in some malicious code...

Although it's a different issue, I absolutely agree. I thought of it recently. But for now, I do not know a better way to evaluate a condition. If you have something in mind, I would appreciate your suggestion. But let's proceed in another issue.

rguliev avatar Dec 03 '18 14:12 rguliev

By the way, your project seems very interesting. Can you share a link (if possible)?

rguliev avatar Dec 03 '18 14:12 rguliev

This issue is fixed in the conditionize2.js Also, take a look at my last comment here

rguliev avatar Apr 15 '19 08:04 rguliev