Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Olá pessoal estou precisando de ajuda para adicionar de forma dinamica um evento em um imput que estou criando.
function addInput(id){ var e = document.getElementById(id); var input1 = document.createElement('input'); input1.setAttribute('type',"text"); input1.setAttribute('id','cadFamiliaDesc'); //aqui vai o erro input1.attachEvent("onBlur", teste); e.appendChild(input1); } function teste(){ alert("isto e um teste!!!"); }
então pessoal estou utilizando o IE e não ocorre erro de javascript porém o evento não e adicionado ao input. Obrigado desde já pela ajuda!!!function addInput(id){ var e = document.getElementById(id); var input1 = document.createElement('input'); input1.setAttribute('type',"text"); input1.setAttribute('id','cadFamiliaDesc'); input1.onblur = function() { alert("isto é um teste!!!"); }; input1.onclick = function() { alert("isto é um outro teste!!!"); }; e.appendChild(input1);}
Valeu Fabiodesign!!!! funcionou perfeitamente, obrigado!
O pior e que fui fazer para um botão {input type="button"}, adicionando o botão e o evento a ele, porém so funcionou com 'onblur' para todos os demais eventos não funciona (principalmente 'onClick' que eu mais preciso), como será que posso corrigir esse problema???
Bom vou disponibilizar uma função que eu criei.
//===========================================================// Validação de tipos//===========================================================isNull = function(x){ if((x == 'undefined') || (x == null)){return true;} else{return false;}};isObject = function(x){ if(!isNull(x)) { if(x.constructor == Object){return true;} else{return false;} } else{return false;}};isFunction = function(x){ if(!isNull(x)) { if(x instanceof Function){return true;} else{return false;} } else{return false;}}isBoolean = function(x){ if(!isNull(x)) { if(x.constructor == Boolean){return true;} else{return false;} } else{return false;}};isArray = function(x){ if(!isNull(x)) { if(x.constructor == Array){return true;} else{return false;} } else{return false;}};isString = function(x){ if(!isNull(x)) { if(x.constructor == String){return true;} else{return false;} } else{return false;}};isDate = function(x){ if(!isNull(x)) { if(x.constructor == Date){return true;} else{return false;} } else{return false;}};isNumber = function(x){ if(!isNull(x)) { if(!isNaN(x) && (x.constructor != Boolean) && (x.constructor != Array)){return true;} else{return false;} } else{return false;}};isInteger = function(x){ if(!isNull(x)) { if(isNumber(x)) { if((x%1) == 0){return true;} else{return false;} } else{return false;} } else{return false;}};//==========================================================// Campo input//==========================================================inputTag = function(p){ var parent = (isString(p)) ? document.getElementById(p) : null; if(isNull(parent)){document.write("Container inválido!");return;} var id = ""; var name = ""; var lbl = null; var type = "text"; var label = ""; var src = ""; var size = ""; var maxlength = ""; var value = ""; var classe = ""; var title = ""; var alt = ""; var input = document.createElement("input"); this.setId = function(x){id = (isString(x)) ? x : "";}; this.setName = function(x){name = (isString(x)) ? x : "";}; this.setType = function(x) { type = x; if(!isString(type) || (type != "text" && type != "password" && type != "file" && type != "checkbox" && type != "radio" && type != "image" && type != "button" && type != "reset" && type != "submit" && type != "hidden")) {type = "text";} }; this.setLabel = function(x) { label = (isString(x)) ? x : ""; var textNode; if(label != "") { textNode = document.createTextNode(label); lbl = document.createElement("label"); if(id != ""){lbl.setAttribute("for",id);} lbl.appendChild(textNode); } }; this.setSrc = function(x){src = (isString(x)) ? x : "";}; this.setSize = function(x){size = (isString(x) || isNumber(x)) ? x : "";}; this.setMaxlength = function(x){maxlength = (isString(x) || isNumber(x)) ? x : "";}; this.setValue = function(x){value = (isString(x) || isNumber(x)) ? x : "";}; this.setClasse = function(x){classe = (isString(x)) ? x : "";}; this.setTitle = function(x){title = (isString(x)) ? x : "";}; this.setAlt = function(x){alt = (isString(x)) ? x : "";}; this.setDisabled = function(x){if(isBoolean(x) && x){input.disabled = x;}}; this.setReadonly = function(x){if(isBoolean(x) && x){input.readOnly = x}}; this.setChecked = function(x){if(isBoolean(x) && x){input.checked = x}}; this.setEvent = function(e,f) { if(isFunction(f)) { if(input.addEventListener) input.addEventListener(e, f, true) if(input.attachEvent) input.attachEvent("on"+e, f) } }; this.draw = function() { input.setAttribute("id",id); input.setAttribute("name",name); input.setAttribute("type",type); input.setAttribute("value",value); input.className = classe; input.setAttribute("title",title); input.setAttribute("alt",alt); if(type == "text" || type == "password"){input.setAttribute("size",size);input.setAttribute("maxlength",maxlength);} if(type == "file"){input.setAttribute("size",size);} if(type == "image"){input.src = src;} if(!isNull(lbl) && type != "radio" && type != "checkbox") { parent.appendChild(lbl); } parent.appendChild(input); if(!isNull(lbl) && (type == "radio" || type == "checkbox")) { parent.appendChild(lbl); } };};
Forma de uso:
<form id="testeTag"><script language="javascript"> var input = new inputTag("testeTag"); input.setId("teste"); input.setLabel("Texto:"); input.setType("text"); input.setSize(50); input.setMaxlength(50); input.setEvent("click",function(){alert("Evento onclick");}); input.setEvent("keypress",function(){alert("Evento onkeypress");}); input.draw();</script></form>
tente assimfunction addInput(id){ var e = document.getElementById(id); var input1 = document.createElement('input'); input1.setAttribute('type',"text"); input1.setAttribute('id','cadFamiliaDesc'); /--------/ input1.setAttribute('onblur',teste); e.appendChild(input1);}function teste(){ alert("isto e um teste!!!");}espero ter ajudado
>
Olá pessoal estou precisando de ajuda para adicionar de forma dinamica um evento em um imput que estou criando.
function addInput(id){ var e = document.getElementById(id); var input1 = document.createElement('input'); input1.setAttribute('type',"text"); input1.setAttribute('id','cadFamiliaDesc'); //aqui vai o erro input1.attachEvent("onBlur", teste); e.appendChild(input1); } function teste(){ alert("isto e um teste!!!"); } então pessoal estou utilizando o IE e não ocorre erro de javascript porém o evento não e adicionado ao input. Obrigado desde já pela ajuda!!!