I’m creating a toggle switch with jquery but still don’t understand how to trigger anything with this toggle. So I have this:
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css" />
<script language="javascript" type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script>
<script src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.js"></script>
</head>
<body>
<div data-role="page" id="page1">
<div data-role="content">
<div data-role="fieldcontain">
<?php
for($i=0;$i<$num_cameras;$i++)
{
?>
<select name="<?php echo "toggleswitch".$i; ?>" id="<?php echo "toggleswitch".$i; ?>" data-theme="" data-role="slider">
<option value="off">Off</option>
<option value="on">On</option>
</select>
<label for="<?php echo "toggleswitch".$i; ?>">
<?php echo $cameras[$i]["camera_name"]; ?>
</label>
<?php
}
?>
</div>
</div>
</div>
<?php
}
}
?>
</body>
I’m getting the list of cameras from the server so I don’t know if that complicates things. What I’d like to do is start playing a video when someone clicks the toggle to “On”. I’m not understanding what I need to do from the jquery docs on the flip toggle switch.
The output looks like this:
<select name="toggleswitch0" id="toggleswitch0" data-theme="" data-role="slider">
<option value="off">Off</option>
<option value="on">On</option>
</select>
<label for="toggleswitch0">Default camera</label>
<select name="toggleswitch1" id="toggleswitch1" data-theme="" data-role="slider">
<option value="off">Off </option>
<option value="on">On</option>
</select>
<label for="toggleswitch1">test</label>
Essentially creates a number of toggle switches based on how many cameras I find, in this case 2. I just really don’t know where to go from here to active these toggles to do something (in my case play a video).
Try using the .change() method along with .val();
Here is an example http://jsfiddle.net/codaniel/STLvS/1/
Note: .on() will only work with jQuery 1.7 + use bind or delegate instead if you have earlier version of jQuery.