Skip to content

Commit

Permalink
use input default checked value is not selected setting specified
Browse files Browse the repository at this point in the history
  • Loading branch information
Janic Beauchemin committed Jul 22, 2014
1 parent adf9b22 commit 1f2a065
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 42 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
jquery.switcher v1.1.1
jquery.switcher v1.1.2
===============

Custom checkbox / radio button in jquery
Expand Down Expand Up @@ -34,13 +34,13 @@ $(function () {

### Params

| Param | Default value | Info |
| ------------- | ------------- | -----------------------------------------------------------------------------|
| class | 'switcher' | class added to the html element. Useful if you want to customize it with css |
| selected | false | if the input is checked or not |
| language | 'en' | default language used for the yes/no texts |
| disabled | false | if the input is disabled |
| copy | {obj} | json object with all the languages. By default, it only contain fr/en |
| Param | Default value | Info |
| ------------- | ------------- | ----------------------------------------------------------------------------------------------|
| class | 'switcher' | class added to the html element. Useful if you want to customize it with css |
| selected | false | if the input is checked or not. If nothing is specified, it will take the input checked value |
| language | 'en' | default language used for the yes/no texts |
| disabled | false | if the input is disabled |
| copy | {obj} | json object with all the languages. By default, it only contain fr/en |

#### You can change the parameters in the javascript when instantiating the plugin

Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jquery.switcher",
"version": "1.1.1",
"version": "1.1.2",
"homepage": "http://djanix.github.io/jquery.switcher/",
"docs": "http://amsul.github.io/pickadate.js",
"authors": [
Expand Down
4 changes: 2 additions & 2 deletions dist/switcher.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* jquery.switcher - 1.1.1
* Copyright (c) 2014-07-17 Janic Beauchemin - https://github.com/djanix/ */
/* jquery.switcher - 1.1.2
* Copyright (c) 2014-07-22 Janic Beauchemin - https://github.com/djanix/ */


.switcher {
Expand Down
29 changes: 17 additions & 12 deletions dist/switcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
* Under MIT License
*/

/* jquery.switcher - 1.1.1
/* jquery.switcher - 1.1.2
* Copyright (c) 2014-07-11 Janic Beauchemin - https://github.com/djanix/ */

;(function ($, window, document, undefined) {
var pluginName = "switcher";

var defaults = {
class: "switcher",
selected: false,
selected: null,
language: "en",
disabled: false,
copy: {
Expand Down Expand Up @@ -92,14 +92,22 @@
},

setValue: function (val) {
if (typeof val != 'boolean') {
return console.log('The parameter need to be true or false as a boolean');
}

var self = this;
var $input = $(self.input);
var $container = $(self.container);

if (val == null) {
if ($input.attr('type') == 'radio') {
val = typeof $input.filter(':checked').val()!=='undefined';
} else {
val = $input.prop('checked');
}
}

if (typeof val != 'boolean') {
return console.log('The parameter need to be true or false as a boolean');
}

self.settings.selected = val;
$input.prop("checked", val);

Expand All @@ -108,15 +116,12 @@
var $inputGroup = $('input[name="' + name + '"]');
var $containerGroup = $inputGroup.parent('.' + self.settings.class);

$containerGroup.removeClass('is-active');
$inputGroup.prop("checked", false);

if (val === true) {
$containerGroup.removeClass('is-active');
$inputGroup.prop("checked", false);

$container.addClass('is-active');
$input.prop("checked", true);
} else {
$containerGroup.first().addClass('is-active');
$inputGroup.first().prop("checked", true);
}
} else {
if (val === true) {
Expand Down
4 changes: 2 additions & 2 deletions dist/switcher.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions dist/switcher.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@
<body>
<h1>Jquery Switcher</h1>

<input type="checkbox" name="switchName" value="switchValue" />
<h2>input</h2><br/>
<input type="checkbox" name="switchName" value="switchValue"/><br/><br/>

<h2>radio</h2><br/>
<input type="radio" name="group1" value="test1" checked>
<input type="radio" name="group1" value="test2">
<input type="radio" name="group1" value="test3">

<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="./dist/switcher.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jquery.switcher",
"version": "1.1.1",
"version": "1.1.2",
"description": "",
"homepage": "",
"author": {
Expand Down
29 changes: 17 additions & 12 deletions switcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
* Under MIT License
*/

/* jquery.switcher - 1.1.1
/* jquery.switcher - 1.1.2
* Copyright (c) 2014-07-11 Janic Beauchemin - https://github.com/djanix/ */

;(function ($, window, document, undefined) {
var pluginName = "switcher";

var defaults = {
class: "switcher",
selected: false,
selected: null,
language: "en",
disabled: false,
copy: {
Expand Down Expand Up @@ -92,14 +92,22 @@
},

setValue: function (val) {
if (typeof val != 'boolean') {
return console.log('The parameter need to be true or false as a boolean');
}

var self = this;
var $input = $(self.input);
var $container = $(self.container);

if (val == null) {
if ($input.attr('type') == 'radio') {
val = typeof $input.filter(':checked').val()!=='undefined';
} else {
val = $input.prop('checked');
}
}

if (typeof val != 'boolean') {
return console.log('The parameter need to be true or false as a boolean');
}

self.settings.selected = val;
$input.prop("checked", val);

Expand All @@ -108,15 +116,12 @@
var $inputGroup = $('input[name="' + name + '"]');
var $containerGroup = $inputGroup.parent('.' + self.settings.class);

$containerGroup.removeClass('is-active');
$inputGroup.prop("checked", false);

if (val === true) {
$containerGroup.removeClass('is-active');
$inputGroup.prop("checked", false);

$container.addClass('is-active');
$input.prop("checked", true);
} else {
$containerGroup.first().addClass('is-active');
$inputGroup.first().prop("checked", true);
}
} else {
if (val === true) {
Expand Down

0 comments on commit 1f2a065

Please sign in to comment.