[SOLVED] I can't trigger event in toggle changes?


#1

As the title of this thread, I stuck in several days and has consulted many articles on this issue. Might you check my code and give me some advise? I use Framework7 V4. Thank you so much.
This is my toggle.html file:

<div class = "view view-init">
<div class = "page">
<ul>
<li>
<div>Toggle1<label class="toggle toggle-init" id="toggle1"><input type="checkbox"/><span class="toggle-icon"></span></label></div>
</li>
</ul>
</div>
</div>

and this is my app.js file:

import $$ from 'dom7';
var app = new Framework7({...});
$$('#toggle1').on('change', function () {
  app.dialog.alert("Hooray");
});

#2

No issues https://jsfiddle.net/4w6Lzrtd/


#3

Thank you but at my side I still can’t trigger the toggle even after your confirmation that code was right. I am really very confused. Might you give some advise in this case, such as should i change code in another way, check code somewhere…?
Thank you agan.


#4

Show the full related code of your app or better live example


#6

After few days working, I have been stucking in that issue. This is my live code https://jsfiddle.net/4h08aq5c/39/ . Could you teach me how to fix that please sir?.
In this code, my ideal is I want to click on “Click this to popup”-link in home page, then app will call up one popup. After that, clicking on “Go for the toggle”-link, app will route to toggle page via function: app.views.current.router.navigate(’/toggle/’). Finally if I change the toggle, one arlet will called.
Thank you for your time and wish you best.


#7

You have everything wrong in your Fiddle. Here is the rule number one:

You should attach any DOM event listeners on elements that are currently in DOM.

There is no toggle in DOM when you write that code, there is no popup in dom when you try to create popup view. Use live listeners, or better put code in pageInit handler for pages. Even better use route components and keep all related code in single files: https://jsfiddle.net/s2kc3jgy/


#8

There are no words to show my appreciation. Right now, I honestly can’t understand all the knowledge you’ve just conveyed to me because I’m not a coder, just a lawyer who wants to create an app to support myself in my work so I ask for permission copy your code and I will continue to learn more. You really made my day and have a good day to you. Thank you from the bottom of my heart.