John Herdean
Posts: 0
Joined: Tue May 14, 2013 3:56 am

How to determine the state of a toggle switch?

Hi, I would like to determine the state of a toggle switch and do something if its toggled, for example:

---------------------------------------------------------------

var a = mobiletoggle_71;
if (a == toggled) {
Appery('mobilebutton_76').hide();
}
else Appery('mobilebutton_76').show();

// mobiletoggle_71 is the name of the toggle switch

--------------------------------------------------------------------

I need clarification on this because Im not sure what is the correct synatx for the lines 1 and 2 are? Thanks.

John Herdean
Posts: 0
Joined: Tue May 14, 2013 3:56 am

How to determine the state of a toggle switch?

ohh and what is the actual value of the toggle switch? for On and for Off? thanks

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

How to determine the state of a toggle switch?

This should be your guide: http://docs.appery.io/javascript-api/, plus jQuery Mobile docs. You need to use Appery("component_name").val().

John Herdean
Posts: 0
Joined: Tue May 14, 2013 3:56 am

How to determine the state of a toggle switch?

ok that solves line 1, but what about line 2?

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

How to determine the state of a toggle switch?

a == 'on'

John Herdean
Posts: 0
Joined: Tue May 14, 2013 3:56 am

How to determine the state of a toggle switch?

never mind, i think i got it. The value is whatever text i put in. Thats what i thought, i needed to be sure.

John Herdean
Posts: 0
Joined: Tue May 14, 2013 3:56 am

How to determine the state of a toggle switch?

Hi again, so Im playing with the toggle switch and making progress, but Im stuck on a couple issues: 1) it doesnt turn back to on when value changes; and 2) its not storing the correct value into localSTorage for me. So, heres my final code that Im running on Value Change:

-----------------------------------------------------------------

var a = Appery('pro19_auto_checkin');
localStorage.setItem('pro19_auto_checkin', a);

if (a == 'on') {
Appery('change_on_off').hide();
}
else Appery('change_on_off').show();

-----------------------------------------------------------------

1):
So, what it does is that lets say the initial state is On. Then I toggle it to Off. That part works and hides 'change_on_off' but when I try to turn the toggle back to On it does not hide 'change_on_off'

Is there an issue running the JS off of Value Change or there a better event to run it off of? Or is there something wrong with my code?

2):
The value of localStorageItem 'pro19_auto_checkin' is always '[object Object]' - I need it to be either 'on' or 'off' depending on the state. WHat is wrong here and why am I getting such an odd value? And how do i fix this issue?

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

How to determine the state of a toggle switch?

Hello! Please use codevar a = Appery('pro19_auto_checkin').val();/code instead of codevar a = Appery('pro19_auto_checkin');/code

John Herdean
Posts: 0
Joined: Tue May 14, 2013 3:56 am

How to determine the state of a toggle switch?

Great, that did the trick for both of my problems. Killed two birds with one stone, way to go :)

John Herdean
Posts: 0
Joined: Tue May 14, 2013 3:56 am

How to determine the state of a toggle switch?

Ok, now that I got the basics down, I need to read from localStorage to determine what the state of the toggle switch should be on Load. I tried to set the property from local storage where i have the value to be 'off' and when the page loads the toggle is still On. Is there something else i need to do to toggle On/Off from local storage on Load?

Image

Return to “Issues”