elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Como proteger una cartera - billetera de Bitcoin


+  Foro de elhacker.net
|-+  Programación
| |-+  Desarrollo Web (Moderador: #!drvy)
| | |-+  Error Cannot set property 'disabled' of null
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Error Cannot set property 'disabled' of null  (Leído 3,378 veces)
SrTrp


Desconectado Desconectado

Mensajes: 325


Script/C#


Ver Perfil
Error Cannot set property 'disabled' of null
« en: 24 Julio 2021, 06:47 am »

Hola que tal estoy intentando llamar una función en JS pero me da ese error
Código:
Cannot set property 'disabled' of null
Este es mi codigo html
Código
  1. <div style="display: table-cell;width: 50%;">
  2.                                <button  id="btn-one" style="background: #036586;border-style: none;border-radius: 10px;padding: 10px;width: 100%;font-size: 16px;" onclick="tp_cits(1);">Tipo 1</button>
  3.                            </div>
  4.                            <div style="display: table-cell;width: 50%;">
  5.                                <button  id="btn_two" style="background: #036586;border-style: none;border-radius: 10px;padding: 10px;width: 100%;margin-left: 10px;font-size: 16px;" onclick="tp_cits(0);">Tipo 2</button>
  6.                            </div>
  7.  
y Aquí esta mi JS
Código
  1. function tp_cits(tipo){
  2.  if(tipo == 0){
  3. document.getElementById("btn-two").disabled = true;
  4.    document.getElementById("btn-one").disabled = false;
  5.    document.getElementById("preciotxt").innerHTML="<strong>Precio: </strong>2000";
  6.  
  7.  }else if(tipo == 1){
  8.    document.getElementById("btn-two").disabled = false;
  9.    document.getElementById("btn-one").disabled = true;
  10.    document.getElementById("preciotxt").innerHTML="<strong>Precio: </strong>1000";
  11.  
  12.  }
  13. }
  14.  
Lo que quiero es que un botón se inhabilite cuando seleccione el otro, leí que esto puede ser por que aun no carga la pagina probé solucionar esto con este código:
Código
  1. $(document).ready(function(){
  2. function tp_cits(tipo){
  3.  if(tipo == 0){
  4. document.getElementById("btn-two").disabled = true;
  5.    document.getElementById("btn-one").disabled = false;
  6.    document.getElementById("preciotxt").innerHTML="<strong>Precio: </strong>2000";
  7.  }else if(tipo == 1){
  8.    document.getElementById("btn-two").disabled = false;
  9.    document.getElementById("btn-one").disabled = true;
  10.    document.getElementById("preciotxt").innerHTML="<strong>Precio: </strong>1000";
  11.  
  12.  }
  13. }
  14. }
  15. });
  16.  
Pero con eso me da este error :
Código:
tp_cits is not defined


En línea

3n31ch


Desconectado Desconectado

Mensajes: 445


Grandes conocimientos engendran grandes dudas


Ver Perfil
Re: Error Cannot set property 'disabled' of null
« Respuesta #1 en: 24 Julio 2021, 07:15 am »

El javascript está cargado, de eso no te preocupes. Tu problema es mas simple, la id de tu botón es "btn_two" no "btn-two". El error Cannot set property 'disabled' of null, es porque estás intentando cambiar la propiedad de algo que no existe (#btn-two no existe y por tanto, getElementById regresa null, null.disabled no existe).
Suerte!

(Por cierto, no recomiendo seguir usando jquery, javascript ya proporciona todas las herramientas que antiguamente solo entregaba jquery).


« Última modificación: 24 Julio 2021, 07:17 am por 3n31ch » En línea

SrTrp


Desconectado Desconectado

Mensajes: 325


Script/C#


Ver Perfil
Re: Error Cannot set property 'disabled' of null
« Respuesta #2 en: 24 Julio 2021, 07:39 am »

Muchas muchas gracias si era eso, que tonto fui con razón siempre me marcaba error en el mismo boton
El javascript está cargado, de eso no te preocupes. Tu problema es mas simple, la id de tu botón es "btn_two" no "btn-two". El error Cannot set property 'disabled' of null, es porque estás intentando cambiar la propiedad de algo que no existe (#btn-two no existe y por tanto, getElementById regresa null, null.disabled no existe).
Suerte!

(Por cierto, no recomiendo seguir usando jquery, javascript ya proporciona todas las herramientas que antiguamente solo entregaba jquery).
En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Fatal error: Cannot access empty property in...
PHP
Thaorius 2 4,280 Último mensaje 30 Diciembre 2006, 06:09 am
por Thaorius
Error null en JSON
Java
javirk 0 1,731 Último mensaje 5 Marzo 2013, 17:34 pm
por javirk
(Ayuda) La función wglMakeCurrent(NULL, NULL) crashea el programa a veces
Programación C/C++
Seyro97 1 1,871 Último mensaje 10 Junio 2015, 15:34 pm
por Eternal Idol
error Cannot read property 'style' of null
Desarrollo Web
SrTrp 2 2,959 Último mensaje 26 Diciembre 2017, 00:41 am
por SrTrp
javascript Error: Cannot read property '1' of undefined
Desarrollo Web
SrTrp 2 2,705 Último mensaje 9 Octubre 2020, 00:42 am
por SrTrp
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines