no-jquery |
function FakeRadio(element) {
this.element = element;
this.name = element.getAttribute('name');
this.fake = this.createFake();
this.radio = this.getAllRadio(this.getClosestForm(element), this.name);
var self = this,
update = function() {
with (self) {
update();
}
};
element.style.display = 'none';
if (element.checked)
this.fake.setAttribute('class', 'fake-radio active');
this.element.onchange = update;
this.fake.onclick = update;
return this;
}
FakeRadio.prototype = {
createFake: function() {
var element = this.element
.parentNode
.insertBefore(document.createElement('span'), this.element);
element.setAttribute('class', 'fake-radio')
return element;
},
getClosestForm: function(element) {
if (element.nodeName === "FORM")
return element;
else
return this.getClosestForm(element.parentNode);
},
getAllRadio: function(form, name) {
var elements = [],
input = form.getElementsByTagName('input');
for (var i = 0; i < input.length; i+= 1) {
var type = input[i].getAttribute('type').toLowerCase(),
inputName = input[i].getAttribute('name');
if (type === 'radio' && inputName === name)
elements.push(input[i]);
}
return elements;
},
update: function() {
for (var i = 0; i < this.radio.length ; i += 1) {
if (this.radio[i].isEqualNode(this.element))
continue;
this.radio[i].cheked = false;
this.radio[i].previousSibling.setAttribute('class', 'fake-radio');
}
this.element.checked = true;
this.fake.setAttribute('class', 'fake-radio active');
}
}
jQuery.fn.fakeRadio = function() {
return jQuery(this).each(function() {
new FakeRadio(this);
});
};
| ready |
jquery | function FakeRadio(element) {
var element = jQuery(element),
name = element.attr('name'),
fake = jQuery('<span class="fake-radio" />').insertAfter(element),
radio = element.closest('form').find('[name="' + name + '"]:radio');
function update() {
radio.each(function() {
jQuery(this)
.prop('checked', false)
.next('.fake-radio')
.removeClass('active');
});
fake.addClass('active')
.prev(':radio')
.prop('checked', true);
}
element.on('change', update);
fake.click(update);
if (element.is(':checked'))
element.prop('checked', true).next('.fake-radio').addClass('active');
return element;
}
jQuery.fn.fakeRadio = function() {
return jQuery(this).each(function() {
new FakeRadio(this);
});
};
| ready |