Ver Mensaje Individual
  #6 (permalink)  
Antiguo 07/09/2014, 18:14
Avatar de berkeleyPunk
berkeleyPunk
 
Fecha de Ingreso: febrero-2013
Ubicación: México :C
Mensajes: 565
Antigüedad: 12 años, 1 mes
Puntos: 22
Pregunta Respuesta: ¿Cómo obtener valor de atributo DATA (HTML5)?

Cita:
Iniciado por Alexis88 Ver Mensaje
Claro. La idea es ir en orden, en este caso, de adentro hacia afuera. Primero, hallas a la opción, luego, obtienes su valor y, finalmente, lo muestras...
Ok, entendido.

Una última cosa, maestro. Estoy intentando hacer que cuando se elija una opción del select se muestre un <p> con el nombre de la fruta y se oculten los otros <p>.

Como siempre, no tengo dificultad en lograr eso con uso de 1,500 if's. El problema es en hacerlo con pocas líneas de código, digamos con un bucle. Algo así:

Código HTML:
Ver original
  1. <select onchange="x(this.options[this.selectedIndex], this.value)">
  2.        <option>:: Elige una fruta ::</option>
  3.        <option value="0" data-fruta="fruta0">Naranja</option>
  4.        <option value="1" data-fruta="fruta1">Fresa</option>
  5.        <option value="2" data-fruta="fruta2">Uva</option>
  6.     </select>
  7.  
  8.     <a id="fruta0" style="display: none;">Naranja</a>
  9.     <a id="fruta1" style="display: none;">Fresa</a>
  10.     <a id="fruta2" style="display: none;">Uva</a>


Código Javascript:
Ver original
  1. function x(indice, value)
  2.        {
  3.           var propiedadData = indice.dataset.fruta;
  4.  
  5.             var miArray = new Array()
  6.             miArray[0]  = document.getElementById('fruta0');
  7.             miArray[1]  = document.getElementById('fruta1');
  8.             miArray[2]  = document.getElementById('fruta2');
  9.  
  10.  
  11.           for (var i=0; i<miArray.length; i++)
  12.           {
  13.             if (miArray[value].id != propiedadData)
  14.             {
  15.                 miArray[value].style.display = "none";
  16.             }
  17.             else
  18.             {
  19.                 miArray[value].style.display = "block";
  20.                 }
  21.           }
  22.        }

Pero, claro está, no sirve bien. ¿Me ayudas?