{"id":2145,"date":"2018-07-17T22:21:56","date_gmt":"2018-07-18T03:21:56","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=2145"},"modified":"2018-07-17T22:21:56","modified_gmt":"2018-07-18T03:21:56","slug":"jquery-toggle-switches-programmatically","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2018\/07\/17\/jquery-toggle-switches-programmatically\/","title":{"rendered":"jQuery: How to toggle switches programmatically"},"content":{"rendered":"<p>Let&#8217;s say you have some HTML\/CSS switches in place of a traditional checkbox.<\/p>\n<p>jQuery makes it easy to toggle switches on or off. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">function toggle(){\r\n  $('.slider').click();\r\n}<\/pre>\n<p>Which can be called in a button like so:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;button type=\"button\" class=\"btn btn-primary\" onClick=\"toggle();\"&gt;Toggle&lt;\/button&gt;<\/pre>\n<p>For example:<\/p>\n<p class='codepen'  data-height='400' data-theme-id='0' data-slug-hash='yqBRZV' data-default-tab='js,result' data-animations='run' data-editable='' data-embed-version='2'>\nSee the Pen Toggle Switches! (jQuery) by Chris Nielsen (@Chris_Nielsen) on CodePen.<\/p>\n\n<p>&nbsp;<\/p>\n<p>Note: This same jQuery can be triggered without clicking a button by simply calling the function in response to some other trigger or condition.<\/p>\n<p>Here is what the HTML looks like:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;div class=\"box\"&gt;\r\n  &lt;h2&gt;&lt;b&gt;Toggle Switch&lt;\/b&gt;&lt;\/h2&gt;\r\n\r\n&lt;!-- gray square --&gt;\r\n&lt;label class=\"switch\"&gt;\r\n  &lt;input type=\"checkbox\"&gt;\r\n  &lt;span class=\"slider\"&gt;&lt;\/span&gt;\r\n&lt;\/label&gt;\r\n\r\n&lt;!-- orange square --&gt;\r\n&lt;label class=\"switch\"&gt;\r\n  &lt;input type=\"checkbox\" checked&gt;\r\n  &lt;span class=\"slider\"&gt;&lt;\/span&gt;\r\n&lt;\/label&gt;\r\n\r\n&lt;br&gt;&lt;br&gt;\r\n\r\n&lt;!-- gray rounded --&gt;\r\n&lt;label class=\"switch\"&gt;\r\n  &lt;input type=\"checkbox\"&gt;\r\n  &lt;span class=\"slider round\"&gt;&lt;\/span&gt;\r\n&lt;\/label&gt;\r\n\r\n&lt;!-- orange rounded --&gt;\r\n&lt;label class=\"switch\"&gt;\r\n  &lt;input type=\"checkbox\" checked&gt;\r\n  &lt;span class=\"slider round\"&gt;&lt;\/span&gt;\r\n&lt;\/label&gt;\r\n\r\n&lt;br&gt;&lt;br&gt;\r\n&lt;button type=\"button\" class=\"btn btn-primary\" \r\n        onClick=\"toggle();\"&gt;Toggle&lt;\/button&gt;\r\n&lt;\/div&gt;\r\n\r\n\r\n<\/pre>\n<p>And here is the CSS:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"css\">body {\r\n  font-family: 'Open Sans', sans-serif;\r\n  background-color: #673AB7;\r\n}\r\n\r\n.switch {\r\n  position: relative;\r\n  display: inline-block;\r\n  width: 60px;\r\n  height: 30px;\r\n}\r\n\r\n.switch input {display:none;}\r\n\r\n.slider {\r\n  position: absolute;\r\n  cursor: pointer;\r\n  top: 0;\r\n  left: 0;\r\n  right: 0;\r\n  bottom: 0;\r\n  background-color: #ccc;\r\n  -webkit-transition: .4s;\r\n  transition: .4s;\r\n}\r\n\r\n.slider:before {\r\n  position: absolute;\r\n  content: \"\";\r\n  height: 24px;\r\n  width: 24px;\r\n  left: 4px;\r\n  bottom: 3px;\r\n  background-color: white;\r\n  -webkit-transition: .2s;\r\n  transition: .2s;\r\n}\r\n\r\ninput:checked + .slider {\r\n  background-color: #D84315;\r\n}\r\n\r\ninput:focus + .slider {\r\n  box-shadow: 0 0 1px #D84315;\r\n}\r\n\r\ninput:checked + .slider:before {\r\n  -webkit-transform: translateX(28px);\r\n  -ms-transform: translateX(28px);\r\n  transform: translateX(28px);\r\n}\r\n\r\n\/* Rounded sliders *\/\r\n.slider.round {\r\n  border-radius: 28px;\r\n}\r\n\r\n.slider.round:before {\r\n  border-radius: 50%;\r\n}\r\n\r\n.box {\r\n  margin: 25px;\r\n  border: 1px solid #ccc;\r\n  width: 300px;\r\n  padding: 0px 30px 30px 30px;\r\n  background-color: white;\r\n}\r\n\r\nh2 {\r\n  padding-bottom: 25px;\r\n}\r\n\r\n.btn {\r\n  border-radius: 14px;\r\n  border: none;\r\n  padding: 5px 15px 5px 15px;\r\n  font-size: 16px;\r\n}\r\n\r\n.btn {\r\n  border-radius: 14px;\r\n  border: none;\r\n}\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>For more information about the jQuery .click() function, see:<br \/>\n<a href=\"http:\/\/api.jquery.com\/click\/\">.click()<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s say you have some HTML\/CSS switches in place of a traditional checkbox. jQuery makes it easy to toggle switches on or off. For example: function toggle(){ $(&#8216;.slider&#8217;).click(); } Which can be called in a button like so: &lt;button type=&#8221;button&#8221; class=&#8221;btn btn-primary&#8221; onClick=&#8221;toggle();&#8221;&gt;Toggle&lt;\/button&gt; For example: &nbsp; Note: This same jQuery can be triggered without clicking &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2018\/07\/17\/jquery-toggle-switches-programmatically\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">jQuery: How to toggle switches programmatically<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44,128],"tags":[10,105,45,123],"class_list":["post-2145","post","type-post","status-publish","format-standard","hentry","category-javascript-language","category-jquery","tag-css","tag-html","tag-javascript","tag-jquery"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2145","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=2145"}],"version-history":[{"count":4,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2145\/revisions"}],"predecessor-version":[{"id":2149,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2145\/revisions\/2149"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=2145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=2145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=2145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}