Foros del Web » Programando para Internet » Javascript »

Cambiar color background a DIV con checkBox

Estas en el tema de Cambiar color background a DIV con checkBox en el foro de Javascript en Foros del Web. Espero este sea el lugar adecuado para dejar mi post. Tengo un Form con una tabla de 8 filas y 4 columnas. En la columna ...
  #1 (permalink)  
Antiguo 21/05/2010, 12:15
 
Fecha de Ingreso: enero-2002
Mensajes: 1.174
Antigüedad: 22 años, 3 meses
Puntos: 21
Cambiar color background a DIV con checkBox

Espero este sea el lugar adecuado para dejar mi post.
  1. Tengo un Form con una tabla de 8 filas y 4 columnas.
  2. En la columna 1 de cada fila tengo un CheckBox
  3. Deseo que al seleccionar el CheckBox, cambie el color de fondo de la fila completa
Imagino se debe hacer con J.S.

¿Podrían darme una idea al respecto?
  #2 (permalink)  
Antiguo 21/05/2010, 14:23
Avatar de Adler
Colaborador
 
Fecha de Ingreso: diciembre-2006
Mensajes: 4.671
Antigüedad: 17 años, 4 meses
Puntos: 126
Respuesta: Cambiar color background a DIV con checkBox

Hola

Sería algo como esto

Código Javascript:
Ver original
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. window.onload = function() {
  5. cant_tr = document.body.getElementsByTagName('tr');
  6. cant_inpt = document.body.getElementsByTagName('input');
  7.  
  8.     for (i=0; i < cant_tr.length; i++) {
  9.  
  10.         for (j=2; j < cant_inpt.length; j+=3) {
  11.  
  12.             cant_inpt[j].onclick = function() {
  13.                 document.getElementById('td_'+this.id).style.backgroundColor = 'red';
  14.  
  15.             }
  16.         }
  17.     }
  18. }
  19. </script>
  20. </head>
  21. <body>
  22. <table border="1">
  23. <tr id="td_1">
  24. <td><input type="text" value="" /></td><td><input type="text" value="" /></td><td>&nbsp<input type="checkbox" id="1" /></td>
  25. </tr>
  26. <tr id="td_2">
  27. <td><input type="text" value="" /></td><td><input type="text" value="" /></td><td>&nbsp<input type="checkbox" id="2" /></td>
  28. </tr>
  29. <tr id="td_3">
  30. <td><input type="text" value="" /></td><td><input type="text" value="" /></td><td>&nbsp<input type="checkbox" id="3" /></td>
  31. </tr>
  32. </table>
  33. </body>
  34. </html>

Suerte
__________________
Los formularios se envían/validan con un botón Submit
<input type="submit" value="Enviar" style="background-color:#0B5795; font:bold 10px verdana; color:#FFF;" />
  #3 (permalink)  
Antiguo 21/05/2010, 15:38
Avatar de wiwi74  
Fecha de Ingreso: marzo-2008
Mensajes: 515
Antigüedad: 16 años, 2 meses
Puntos: 10
Respuesta: Cambiar color background a DIV con checkBox

Prueba esto:
Checa y descheca:



<!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=iso-8859-1" />
<title>Documento sin t&iacute;tulo</title>
</head>

<body>

<form>
<table border="1" cellpadding="10">
<tr id="1">
<td><input onclick="if(this.checked){
document.getElementById('1').style.backgroundColor ='red';
}else{
document.getElementById('1').style.backgroundColor ='blue';
}"
type="checkbox" /></td>
<td >Contenido</td>
<td >Contenido</td>
<td >Contenido</td>
</tr>

<tr id="2">
<td><input onclick="if(this.checked){
document.getElementById('2').style.backgroundColor ='red';
}else{
document.getElementById('2').style.backgroundColor ='blue';
}"
type="checkbox" /></td>
<td >Contenido</td>
<td >Contenido</td>
<td >Contenido</td>
</tr>

</table>
</form>


</body>
</html>
  #4 (permalink)  
Antiguo 21/05/2010, 16:01
 
Fecha de Ingreso: enero-2002
Mensajes: 1.174
Antigüedad: 22 años, 3 meses
Puntos: 21
Respuesta: Cambiar color background a DIV con checkBox

Gracias, ambos códigos funcionan de acuerdo a lo que requiero. ¡Excente!

Ahora quisiera este mismo efecto, pero con "Boton de Opción" (radio), el cual tiene 5 opciones y asignarle el color de fondo según la opción seleccionada.

Gracias
  #5 (permalink)  
Antiguo 21/05/2010, 16:31
Avatar de wiwi74  
Fecha de Ingreso: marzo-2008
Mensajes: 515
Antigüedad: 16 años, 2 meses
Puntos: 10
Respuesta: Cambiar color background a DIV con checkBox

Bueno pero nada mas por hoy:

Si deseas agregar mas radios fijate en esto:

para el primer radio 1 se pone rojo 2 blanco
onclick="document.getElementById('1').style.backgr oundColor='red';
document.getElementById('2').style.backgroundColor ='white';
si habria un tercero lo ponemos blanco
document.getElementById('3').style.backgroundColor ='white';



para el segundo radio 2 se pone rojo 1 blanco
onclick="document.getElementById('2').style.backgr oundColor='red';
document.getElementById('1').style.backgroundColor ='white';
si habria un tercero lo ponemos blanco
document.getElementById('3').style.backgroundColor ='white';


En sintesis le pone el mismo numero ('aqui') que tiene la fila <tr id='num'> donde se encuantra el radio.



El codigo:




<form>
<table border="1" cellpadding="10">
<tr id="1">
<td><input type="radio"
name="equipo"
value="equipo1"
onclick="document.getElementById('1').style.backgr oundColor='red';
document.getElementById('2').style.backgroundColor ='white';
" />
</td>
<td >Contenido</td>
<td >Contenido</td>
<td >Contenido</td>
</tr>

<tr id="2">
<td><input type="radio"
name="equipo"
value="equipo2"
onclick="document.getElementById('2').style.backgr oundColor='red';
document.getElementById('1').style.backgroundColor ='white';
" />
</td>
<td >Contenido</td>
<td >Contenido</td>
<td >Contenido</td>
</tr>

</table>
</form>
  #6 (permalink)  
Antiguo 21/05/2010, 17:07
 
Fecha de Ingreso: enero-2002
Mensajes: 1.174
Antigüedad: 22 años, 3 meses
Puntos: 21
Respuesta: Cambiar color background a DIV con checkBox

Gracias.
  1. He adaptado un poco el código y trabaja perfecto para mi requerimiento.
  2. En mi aplicación realmente tengo 100 filas y 6 botones radio por fila, lo que hace un total de 600 opciones radio.
  3. Mi aplicación es PHP y puedo asignar el código sin problema

A continuación muestro el código que estoy empleando.

Pregunta: ¿Podría incluirse una función y evitar saturar de código el HTML?

Gracias anticipadas.


Código HTML:
Ver original
  1. <table border="1" cellpadding="10">
  2.  
  3. <tr id="1">
  4. <td>
  5. <input  type="radio" name="equipo1" value="1" onclick="document.getElementById('1').style.backgroundColor='blue';" />
  6. </td>
  7. <td>
  8. <input type="radio" name="equipo1" value="2" onclick="document.getElementById('1').style.backgroundColor='red';" />
  9. </td>
  10. <td ><input type="radio" name="equipo1" value="3" onclick="document.getElementById('1').style.backgroundColor='yellow';" />
  11. </td>
  12. <td >Contenido</td>
  13. </tr>
  14.  
  15. <tr id="2">
  16. <td>
  17. <input  type="radio" name="equipo2" value="1" onclick="document.getElementById('2').style.backgroundColor='blue';" />
  18. </td>
  19. <td>
  20. <input type="radio" name="equipo2" value="2" onclick="document.getElementById('2').style.backgroundColor='red';" />
  21. </td>
  22. <td ><input type="radio" name="equipo2" value="3" onclick="document.getElementById('2').style.backgroundColor='yellow';" />
  23. </td>
  24. <td >Contenido</td>
  25. </tr>
  26.  
  27. <tr id="3">
  28. <td>
  29. <input  type="radio" name="equipo3" value="1" onclick="document.getElementById('3').style.backgroundColor='blue';" />
  30. </td>
  31. <td>
  32. <input type="radio" name="equipo3" value="2" onclick="document.getElementById('3').style.backgroundColor='red';" />
  33. </td>
  34. <td ><input type="radio" name="equipo3" value="3" onclick="document.getElementById('3').style.backgroundColor='yellow';" />
  35. </td>
  36. <td >Contenido</td>
  37. </tr>
  38.  
  39. </form>
  #7 (permalink)  
Antiguo 21/05/2010, 20:25
Avatar de wiwi74  
Fecha de Ingreso: marzo-2008
Mensajes: 515
Antigüedad: 16 años, 2 meses
Puntos: 10
Respuesta: Cambiar color background a DIV con checkBox

Crea un archivo php y metele este codigo dentro de las etiquetas <body></body>
No toques nada, ya funciona, luego si, adaptalo.




Solo fijate en estas 2 lineas, que se genera un espacio " #ff0000" bueno quitalo que da error:
(se generan al darle a guardar, aqui en el foro)
document.getElementById(i).style.backgroundColor=" #ff0000"; //esta es una
document.getElementById(i).style.backgroundColor=" #ffffff"; //esta la otra (estan mas abajo)



El codigo:





<!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=iso-8859-1" />
<title>Documento sin t&iacute;tulo</title>

<script type="text/javascript" language="javascript">
function darColor(paramID){
//alert(document.getElementsByTagName("tr").length);
for(i=0; i<(document.getElementsByTagName("tr").length); i++){
//alert(document.getElementById(i).id);
if(document.getElementById(i).id == paramID){
document.getElementById(i).style.backgroundColor=" #ff0000";
}else{
document.getElementById(i).style.backgroundColor=" #ffffff";
}
}
}
</script>


</head>

<body>


<form action="" method="post">
<table border="1" cellpadding="10">
<?
//CODIGO DINAMICO
for($i=0; $i<10; $i++){

echo'<tr id="'.$i.'"><td>
<input type="radio"
name="equipo"
value="equipo'.$i.'"
onclick="darColor('.$i.')" />
</td>
<td>
Contenido
</td>
</tr>';

}

?>

</table>
<BR /><input type="submit" value="MOSTRAR EL VALOR CHECADO" />
</form>

<?
echo "<br />".$_POST['equipo'];
?>

</body>
</html>

Última edición por wiwi74; 21/05/2010 a las 20:42
  #8 (permalink)  
Antiguo 21/05/2010, 21:08
 
Fecha de Ingreso: enero-2002
Mensajes: 1.174
Antigüedad: 22 años, 3 meses
Puntos: 21
Respuesta: Cambiar color background a DIV con checkBox

Estimado wiwi74,

Tu ayuda ha sido invaluable, gracias.

En base a tu primer propuesta, desarrolle toda la función en PHP, la cual trabaja perfectamente.

Ahora con esta reciente propuesta (con función), será más eficiente el PHP, cabe comentar que lo he desarrollado de manera similar a tu segunda propuesta, por lo que aplicar la función me será de relativa facilidad.

Gracias.
  #9 (permalink)  
Antiguo 21/05/2010, 22:17
Avatar de wiwi74  
Fecha de Ingreso: marzo-2008
Mensajes: 515
Antigüedad: 16 años, 2 meses
Puntos: 10
Respuesta: Cambiar color background a DIV con checkBox

Gracias y suerte...

Etiquetas: checkbox, color, fondo
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 07:12.