Dear Reader, let us start to learn to ‘Get value of jQuery toggle button yes/no‘.
As in today’s era, we are going to be very sophisticated and want to use very less work with more output. So by providing a jquery toggle button Yes/No , we as a front end developer, give the way of sophistication to the end user. Who may be very little educated person like any housekeepers. So by giving these User Interface to end user, they feel very happy. As it is very easy to get input from the end user. And by getting that input from the end user, you can perform the major task of saving that input into the database table as Yes or No. Like whether housekeeper performed his task or not.
So for checking user’s input you have to see the result on Console Window as is_checked :- true or false.
This is just one click Toggle Button.
For that purpose, you need to Get value of jQuery toggle button yes/no.
Step 1. Create an HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<!DOCTYPE html> <html> <head> <script src="https://code.jquery.com/jquery-1.11.3.js"></script> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>Web Preparations</title> </head> <body> <p>Toggle Button Yes/No</p> <label class="switch"> <input class="switch-input" type="checkbox" /> <span class="switch-label" data-on="Yes" data-off="No"></span> <span class="switch-handle"></span> </label> </body> </html> |
Step 2. Add CSS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
<style> .switch { position: relative; display: block; vertical-align: top; width: 100px; height: 30px; padding: 3px; margin: 0 10px 10px 0; background: linear-gradient(to bottom, #eeeeee, #FFFFFF 25px); background-image: -webkit-linear-gradient(top, #eeeeee, #FFFFFF 25px); border-radius: 18px; box-shadow: inset 0 -1px white, inset 0 1px 1px rgba(0, 0, 0, 0.05); cursor: pointer; } .switch-input { position: absolute; top: 0; left: 0; opacity: 0; } .switch-label { position: relative; display: block; height: inherit; font-size: 10px; text-transform: uppercase; background: #eceeef; border-radius: inherit; box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.12), inset 0 0 2px rgba(0, 0, 0, 0.15); } .switch-label:before, .switch-label:after { position: absolute; top: 50%; margin-top: -.5em; line-height: 1; -webkit-transition: inherit; -moz-transition: inherit; -o-transition: inherit; transition: inherit; } .switch-label:before { content: attr(data-off); right: 11px; color: #aaaaaa; text-shadow: 0 1px rgba(255, 255, 255, 0.5); } .switch-label:after { content: attr(data-on); left: 11px; color: #FFFFFF; text-shadow: 0 1px rgba(0, 0, 0, 0.2); opacity: 0; } .switch-input:checked ~ .switch-label { background: #1331DC; box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.15), inset 0 0 3px rgba(0, 0, 0, 0.2); } .switch-input:checked ~ .switch-label:before { opacity: 0; } .switch-input:checked ~ .switch-label:after { opacity: 1; } .switch-handle { position: absolute; top: 4px; left: 4px; width: 28px; height: 28px; background: linear-gradient(to bottom, #FFFFFF 40%, #f0f0f0); background-image: -webkit-linear-gradient(top, #FFFFFF 40%, #f0f0f0); border-radius: 100%; box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.2); } .switch-handle:before { content: ""; position: absolute; top: 50%; left: 50%; margin: -6px 0 0 -6px; width: 12px; height: 12px; background: linear-gradient(to bottom, #eeeeee, #FFFFFF); background-image: -webkit-linear-gradient(top, #eeeeee, #FFFFFF); border-radius: 6px; box-shadow: inset 0 1px rgba(0, 0, 0, 0.02); } .switch-input:checked ~ .switch-handle { left: 74px; box-shadow: -1px 1px 5px rgba(0, 0, 0, 0.2); } /* Transition ========================== */ .switch-label, .switch-handle { transition: All 0.3s ease; -webkit-transition: All 0.3s ease; -moz-transition: All 0.3s ease; -o-transition: All 0.3s ease; } </style> |
Step 3. Add jQuery for making code live
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<script> $(document).ready(function() { $('.switch-input').on('change', function() { var is_checked = $(this).is(':checked'); var selected_data; var $switch_label = $('.switch-label'); console.log('is_checked: ' + is_checked); if(is_checked) { selected_data = $switch_label.attr('data-on'); } else { selected_data = $switch_label.attr('data-off'); } console.log('Selected data: ' + selected_data); }); // Params ($selector, boolean) function set_switch_state(el, flag) { el.attr('checked', flag); } // Usage set_switch_state($('.switch-input'), true); }); </script> |
Final Step :- Gather whole code at one place as jquery_toggle_button.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
<!DOCTYPE html> <html> <head> <script src="https://code.jquery.com/jquery-1.11.3.js"></script> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>Web Preparations</title> <style> .switch { position: relative; display: block; vertical-align: top; width: 100px; height: 30px; padding: 3px; margin: 0 10px 10px 0; background: linear-gradient(to bottom, #eeeeee, #FFFFFF 25px); background-image: -webkit-linear-gradient(top, #eeeeee, #FFFFFF 25px); border-radius: 18px; box-shadow: inset 0 -1px white, inset 0 1px 1px rgba(0, 0, 0, 0.05); cursor: pointer; } .switch-input { position: absolute; top: 0; left: 0; opacity: 0; } .switch-label { position: relative; display: block; height: inherit; font-size: 10px; text-transform: uppercase; background: #eceeef; border-radius: inherit; box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.12), inset 0 0 2px rgba(0, 0, 0, 0.15); } .switch-label:before, .switch-label:after { position: absolute; top: 50%; margin-top: -.5em; line-height: 1; -webkit-transition: inherit; -moz-transition: inherit; -o-transition: inherit; transition: inherit; } .switch-label:before { content: attr(data-off); right: 11px; color: #aaaaaa; text-shadow: 0 1px rgba(255, 255, 255, 0.5); } .switch-label:after { content: attr(data-on); left: 11px; color: #FFFFFF; text-shadow: 0 1px rgba(0, 0, 0, 0.2); opacity: 0; } .switch-input:checked ~ .switch-label { background: #1331DC; box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.15), inset 0 0 3px rgba(0, 0, 0, 0.2); } .switch-input:checked ~ .switch-label:before { opacity: 0; } .switch-input:checked ~ .switch-label:after { opacity: 1; } .switch-handle { position: absolute; top: 4px; left: 4px; width: 28px; height: 28px; background: linear-gradient(to bottom, #FFFFFF 40%, #f0f0f0); background-image: -webkit-linear-gradient(top, #FFFFFF 40%, #f0f0f0); border-radius: 100%; box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.2); } .switch-handle:before { content: ""; position: absolute; top: 50%; left: 50%; margin: -6px 0 0 -6px; width: 12px; height: 12px; background: linear-gradient(to bottom, #eeeeee, #FFFFFF); background-image: -webkit-linear-gradient(top, #eeeeee, #FFFFFF); border-radius: 6px; box-shadow: inset 0 1px rgba(0, 0, 0, 0.02); } .switch-input:checked ~ .switch-handle { left: 74px; box-shadow: -1px 1px 5px rgba(0, 0, 0, 0.2); } /* Transition ========================== */ .switch-label, .switch-handle { transition: All 0.3s ease; -webkit-transition: All 0.3s ease; -moz-transition: All 0.3s ease; -o-transition: All 0.3s ease; } </style> </head> <body> <p>Toggle Button Yes/No</p> <label class="switch"> <input class="switch-input" type="checkbox" /> <span class="switch-label" data-on="Yes" data-off="No"></span> <span class="switch-handle"></span> </label> <script> $(document).ready(function() { $('.switch-input').on('change', function() { var is_checked = $(this).is(':checked'); var selected_data; var $switch_label = $('.switch-label'); console.log('is_checked: ' + is_checked); if(is_checked) { selected_data = $switch_label.attr('data-on'); } else { selected_data = $switch_label.attr('data-off'); } console.log('Selected data: ' + selected_data); }); // Params ($selector, boolean) function set_switch_state(el, flag) { el.attr('checked', flag); } // Usage set_switch_state($('.switch-input'), true); }); </script> </body> </html> |
Congratulations you have successfully Completed to Get value of jQuery toggle button yes/no, if you like this post and was helpful for you then share this post on social media and if you have any query then please contact us or comment below and don’t Forget Like and Follow us on Social Media, Thanks.