Skip to content
This repository has been archived by the owner on Jul 12, 2019. It is now read-only.

Add triggering of events on show and hide #20

Open
landwire opened this issue Nov 29, 2018 · 6 comments
Open

Add triggering of events on show and hide #20

landwire opened this issue Nov 29, 2018 · 6 comments
Assignees

Comments

@landwire
Copy link

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');
      }
    };
@dcblogdev
Copy link

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

@rguliev
Copy link
Collaborator

rguliev commented Dec 1, 2018

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.

@landwire
Copy link
Author

landwire commented Dec 3, 2018

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...

@rguliev
Copy link
Collaborator

rguliev commented Dec 3, 2018

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
Copy link
Collaborator

rguliev commented Dec 3, 2018

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

@rguliev
Copy link
Collaborator

rguliev commented Apr 15, 2019

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants