Ver Mensaje Individual
  #4 (permalink)  
Antiguo 18/03/2012, 09:58
Avatar de Panino5001
Panino5001
Me alejo de Omelas
 
Fecha de Ingreso: mayo-2004
Ubicación: -34.637167,-58.462984
Mensajes: 5.148
Antigüedad: 20 años
Puntos: 834
Respuesta: pasar parametros a funcion onclick

Quizá estos ejemplos te den una idea.
Este está basado en la última opción que te comenté:
Código PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml">
<
head>
<
meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<
title>Documento sin título</title>
<
script type="text/javascript">
function 
agregar_carrito(el) {
//notar que en realidad retornamos una función anónima:
    
return function(){
        
//acá, en lugar del alert, lo que necesites
        
alert(el['nombre']+' '+el['color']+'-->'+el['precio']); 
        return 
false;
    }
}
function 
setItems(){
    var 
items=[{nombre:'mochila',precio:2.5,color:'verde'},{nombre:'cuaderno',precio:0.5,color:'azul'},{nombre:'lápiz',precio:.1,color:'negro'}],i=0,l=items.length,a;
    for(;
i<l;i++){
        
document.createElement('a');
        
a.href='#';
        
a.innerHTML items[i]['nombre']+' '+items[i]['color']+'-->'+items[i]['precio']; 
        (function(
el){a.onclick agregar_carrito(el);})(items[i]);
        
document.body.appendChild(a);
        
document.body.appendChild(document.createElement('br'));
    }
}
onload=setItems;
</script>

</head>

<body>
</body>
</html> 
Este, en cambio, es más parecido a lo que solicitaste en tu último post. Está basado en los setters y getters de atributos. Consiste en añadir atributos customizados a elementos html. Durante mucho tiempo fue considerado una mala práctica y en algunos navegadores, si se usaba mal, podía traer problemas de memoria. Pero en html 5, pueden añadirse atributos data, así que
"Viva la fiesta
Viva la noche
...
Johnny, la gente está muy loca
...
":
Código PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml">
<
head>
<
meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<
title>Documento sin título</title>
<
script type="text/javascript">
function 
agregar_carrito() {
    
/*
    ver en html 5:
    this.dataset.nombre,this.dataset.color y this.dataset.precio
    */
        
alert(this.getAttribute('data-nombre')+' '+this.getAttribute('data-color')+'-->'+this.getAttribute('data-precio'));
        return 
false;
}
function 
setItems(){
    var 
items=[{nombre:'mochila',precio:2.5,color:'verde'},{nombre:'cuaderno',precio:0.5,color:'azul'},{nombre:'lápiz',precio:.1,color:'negro'}],i=0,l=items.length,a;
    for(;
i<l;i++){
        
document.createElement('a');
        
a.href='#';
        
a.innerHTML items[i]['nombre']+' '+items[i]['color']+'-->'+items[i]['precio']; 
        
a.setAttribute('data-nombre',items[i]['nombre']);
        
a.setAttribute('data-color',items[i]['color']);
        
a.setAttribute('data-precio',items[i]['precio']);
        
a.onclick agregar_carrito;
        
document.body.appendChild(a);
        
document.body.appendChild(document.createElement('br'));
    }
}
onload=setItems;
</script>

</head>

<body>
</body>
</html>