Foros del Web » Programando para Internet » Javascript »

evitar "cannot read property 'value' of undefined"

Estas en el tema de evitar "cannot read property 'value' of undefined" en el foro de Javascript en Foros del Web. Pues eso estoy haciendo una cosilla en javascript y me encuentro en un punto que tengo que mirar si una etiqueta tiene un atributo y ...
  #1 (permalink)  
Antiguo 26/12/2011, 08:06
Avatar de juanito1712  
Fecha de Ingreso: mayo-2010
Ubicación: Valencia
Mensajes: 1.124
Antigüedad: 13 años, 11 meses
Puntos: 66
evitar "cannot read property 'value' of undefined"

Pues eso estoy haciendo una cosilla en javascript y me encuentro en un punto que tengo que mirar si una etiqueta tiene un atributo y en caso de no tenerlo mirar en su objeto padre y asi sucesivamente, pero claro en el momento que le digo al programa

e.target.attributes.typex.value

al no contener ese atributo me salta un error y parece que no sigue con la función.

Como puedo solucionar esto?


ahora mismo tengo algo así
Código Javascript:
Ver original
  1. if(e.target.attributes.typex.value){
  2.                 alert(e.target.attributes.typex.value);
  3. }else if(e.target.parentNode.attributes.typex.value){
  4.                 alert(e.target.parentNode.attributes.typex.value);
  5.             }
  #2 (permalink)  
Antiguo 26/12/2011, 14:03
Avatar de IsaBelM
Colaborador
 
Fecha de Ingreso: junio-2008
Mensajes: 5.032
Antigüedad: 15 años, 10 meses
Puntos: 1012
Respuesta: evitar "cannot read property 'value' of undefined"

typex no es un atributo, aun así podemos obtener su valor
Cita:
alert(evt.target.typex) // si no existe, 'undefined'
alert(evt.target.attributes.typex.value) // si no existe, error

<input type="text" value="input1" typex="nuevo attr" />
<input type="text" value="input2" />
__________________
if(ViolenciaDeGénero) {alert('MUJER ASESINADA');}
  #3 (permalink)  
Antiguo 26/12/2011, 16:41
Avatar de juanito1712  
Fecha de Ingreso: mayo-2010
Ubicación: Valencia
Mensajes: 1.124
Antigüedad: 13 años, 11 meses
Puntos: 66
Respuesta: evitar "cannot read property 'value' of undefined"

lo pruebo y me da undefined en los dos casos :S

al final lo he solucionado así
Código Javascript:
Ver original
  1. var objetivo="vacio";
  2.                        for (var attr in e.target.attributes) {
  3.                 if(e.target.attributes[attr].name=="typex"){
  4.                     objetivo=e.target.attributes[attr].value;
  5.                 }
  6.             }
  7.             if(objetivo=="vacio"){
  8.                 for (var attr in e.target.parentNode.attributes) {
  9.                     if(e.target.parentNode.attributes[attr].name=="typex"){
  10.                         objetivo=e.target.parentNode.attributes[attr].value;
  11.                     }
  12.                 }
  13.             }

de la forma que decías tu me había gustado que es menos faena, pero bueno almenos lo he conseguido solucionar...
  #4 (permalink)  
Antiguo 27/12/2011, 06:42
Avatar de IsaBelM
Colaborador
 
Fecha de Ingreso: junio-2008
Mensajes: 5.032
Antigüedad: 15 años, 10 meses
Puntos: 1012
Respuesta: evitar "cannot read property 'value' of undefined"

Cita:
Iniciado por juanito1712 Ver Mensaje
lo pruebo y me da undefined en los dos casos :S
sí, cometí un error
Cita:
evt.target.attributes.typex
y creo que no me expliqué bien. si existe el objeto te retorna "object" si no existe te retorna "undefined". prueba con esto
Cita:
function fnc(e) {

alert(typeof(e.target.attributes.typex));

var objetivo="";

if(typeof(e.target.attributes.typex) == "object"){
objetivo=e.target.attributes.typex.value;
} else {
objetivo=e.target.parentNode.attributes.typex.valu e;
}

alert(objetivo);
}

<form typex="attr form">
<input type="text" value="input1" typex="nuevo attr" onclick="fnc(event)" />
<input type="text" value="input2" onclick="fnc(event)" />
</form>
__________________
if(ViolenciaDeGénero) {alert('MUJER ASESINADA');}
  #5 (permalink)  
Antiguo 27/12/2011, 12:35
Avatar de juanito1712  
Fecha de Ingreso: mayo-2010
Ubicación: Valencia
Mensajes: 1.124
Antigüedad: 13 años, 11 meses
Puntos: 66
Respuesta: evitar "cannot read property 'value' of undefined"

wapoooo

este ha ido casi perfecto y bastante mas liviano que el que y había pesado

muchísimas gracias

Etiquetas: read
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 05:22.