Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Refactoring Code to Work with jQuery 1.3.2

Featured Replies

I have some code that I'm writing for a client and they're currently using Drupal 6 and jQuery 1.3.2 - I have no control over what jQuery they use and so I'm trying to write some code based on that assumption that it won't change any time soon. I've been re-writing the code and now have it working as far back as 1.4 but I've run in to a brick wall trying to get it to work on 1.3.


I'm using FlatUI's radio buttons - original here (https://raw.github.com/designmodo/Flat-UI/master/js/flatui-radio.js) - I've already re-worked it but cannot find what's stopping it from triggering.


I've setup a JSFiddle to demonstrate my problem. Everything is working okay apart from the age selection part where there should be checkboxes at the left of each age (13+, 14+ etc). As I've said, it works as far back as 1.4 and even looking through the jQuery docs, I can't find anything else that shouldn't work on 1.3.


http://jsfiddle.net/dvw9F/



/* =============================================================
* flatui-radio.js v0.0.3
* ============================================================ */

(function ($) {

'use strict';

/* RADIO PUBLIC CLASS DEFINITION
* ============================== */

var Radio = function (element, options) {
this.init(element, options);
};

Radio.prototype = {

constructor: Radio,

init: function (element, options) {

var $el = this.$element = $(element);

this.options = $.extend({}, $.fn.radio.defaults, options);
$el.before(this.options.template);
this.setState();

},

setState: function () {

var $el = this.$element,
$parent = $el.closest('.radio');

if ($el.attr('disabled') === true) {
$parent.addClass('disabled');
}

if ($el.attr('checked') === true) {
$parent.addClass('checked');
}

},

toggle: function () {

var d = 'disabled',
ch = 'checked',
$el = this.$element,
checked = false,
$parent = $el.closest('.radio'),
$parentWrap = $el.closest('form').length ? $el.closest('form') : $el.closest('body'),
$elemGroup = $parentWrap.find(':radio[name="' + $el.attr('name') + '"]'),
e = $.Event('toggle');

if ($el.attr(ch) === true) {
checked = true;
}

$elemGroup.not($el).each(function () {

var $el = $(this),
$parent = $(this).closest('.radio');

if ($el.attr(d) !== true) {
$parent.removeClass(ch) && $el.removeAttr(ch).trigger('change');
}

});

if ($el.attr(d) !== true) {

if (checked === false) {
$parent.addClass(ch) && $el.attr(ch, true);
}

$el.trigger(e);

if (true !== $el.attr(ch)) {
$el.trigger('change');
}

}

},

setCheck: function (option) {

var ch = 'checked',
$el = this.$element,
$parent = $el.closest('.radio'),
checkAction = option === 'check' ? true : false,
checked = false,
$parentWrap = $el.closest('form').length ? $el.closest('form') : $el.closest('body'),
$elemGroup = $parentWrap.find(':radio[name="' + $el.attr('name') + '"]'),
e = $.Event(option);

if ($el.attr(ch) === true) {
checked = true;
}

$elemGroup.not($el).each(function () {

var $el = $(this),
$parent = $(this).closest('.radio');

$parent.removeClass(ch) && $el.removeAttr(ch);

});

$parent[checkAction ? 'addClass' : 'removeClass'](ch) && checkAction ? $el.attr(ch, ch) : $el.removeAttr(ch);
$el.trigger(e);

if (checked !== $el.attr(ch)) {
$el.trigger('change');
}

}

};

/* RADIO PLUGIN DEFINITION
* ======================== */

var old = $.fn.radio;

$.fn.radio = function (option) {

return this.each(function () {

var $this = $(this),
data = $this.data('radio'),
options = $.extend({}, $.fn.radio.defaults, $this.data(), typeof option === 'object' && option);

if (!data) {
$this.data('radio', (data = new Radio(this, options)));
}

if (option === 'toggle') {
data.toggle();
}

if (option === 'check' || option === 'uncheck') {
data.setCheck(option);
} else if (option) {
data.setState();
}

});

};

$.fn.radio.defaults = {
template: '<span class="icons"><span class="first-icon fui-radio-unchecked"></span><span class="second-icon fui-radio-checked"></span></span>'
};

/* RADIO NO CONFLICT
* ================== */

$.fn.radio.noConflict = function () {

$.fn.radio = old;
return this;

};

/* RADIO DATA-API
* =============== */

$('.radio').live('click', '[data-toggle^=radio], .radio', function (e) {

var $radio = $(e.target);

if (e.target.tagName !== "A") {

e && e.preventDefault() && e.stopPropagation();
if (!$radio.hasClass('radio')) {
$radio = $radio.closest('.radio');
}

$radio.find(':radio').radio('toggle');

}

});

$(function () {

$('[data-toggle="radio"]').each(function () {
var $radio = $(this);
$radio.radio();
});

});

}(jQuery));

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.