DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Reemplazar Imagen Del Checkbox De HTML

11.12.2008
| 1814 views |
  • submit to reddit
        
var inputs;
//Acá van las URL de las imagenes
var imgFalse = "/images/chk_off.gif";
var imgTrue = "/images/chk_on.gif";
 
function init() {
    replaceChecks();
}
 
function replaceChecks() {
 
    inputs = document.getElementsByTagName('input');
 
    for(var i=0; i < inputs.length; i++) {
 
        if(inputs[i].getAttribute('type') == 'checkbox') { 
 
            var img = document.createElement('img');
 
            if(inputs[i].checked) {
                img.src = imgTrue;
            } else {
                img.src = imgFalse;
            }
            img.onclick = inputs[i].onclick;
            img.id = 'checkImage'+i;
            funct = new Function('checkChange('+i+')');
            addFunctionToFunction(img, funct)
            inputs[i].parentNode.insertBefore(img, inputs[i]);
 
            inputs[i].style.display='none';
        }
    }
}
function addFunctionToFunction(obj, funct) {
  var oldonclick = obj.onclick;
  if (typeof obj.onclick != 'function') {
    obj.onclick = funct;
  } else {
    obj.onclick = function() {
      if (oldonclick) {
        oldonclick();
      }
      funct();
    }
  }
}
function checkChange(i) {
 
    if(inputs[i].checked) {
        inputs[i].checked = '';
        document.getElementById('checkImage'+i).src=imgFalse;
    } else {
        inputs[i].checked = 'checked';
        document.getElementById('checkImage'+i).src=imgTrue;
    }
}

window.onload = init;