Ver Mensaje Individual
  #5 (permalink)  
Antiguo 14/11/2013, 12:55
Avatar de marlanga
marlanga
 
Fecha de Ingreso: enero-2011
Ubicación: Murcia
Mensajes: 1.024
Antigüedad: 13 años, 3 meses
Puntos: 206
Respuesta: Boton mas y menos

A los elementos button no tienes que ponerles el atributo 'type="button"'.
El código, parece que funciona, pero a mi no me gusta nada que se meta código javascript dentro de los elementos HTML, como por ejemplo haces con el atributo "onclick" (al igual que tampoco nadie recomienda que se meta estilos CSS en ellos).
Si tu código HTML crece, y/o necesitas modificar códigos en el futuro, si sigues así será una locura hacerlo.

Yo lo cambiaría por:
http://jsfiddle.net/5WdvA/
Código HTML:
Ver original
  1. <button id="btn1">-</button><input type="text" id="txt" /><button id="btn2">+</button>
Código Javascript:
Ver original
  1. window.onload=function(){
  2.     var sumar=function(cantidad,txt){
  3.         txt.value=(!isNaN(txt.value) && txt.value!="")?parseInt(txt.value)+cantidad:0;
  4.     }
  5.     var SUMADOR1={
  6.         btnResta: document.getElementById("btn1"),
  7.         btnSuma: document.getElementById("btn2"),
  8.         txt: document.getElementById("txt")
  9.     };
  10.     SUMADOR1.btnResta.onclick=function(){ sumar(-1,SUMADOR1.txt); };
  11.     SUMADOR1.btnSuma.onclick=function(){ sumar(1,SUMADOR1.txt); };
  12. }