Foros del Web » Programando para Internet » Javascript »

modificar select

Estas en el tema de modificar select en el foro de Javascript en Foros del Web. buenas noches a todos, estoy buscando ayuda con un select al cual se le a dado un estilo pero este no me modifica el elemento ...
  #1 (permalink)  
Antiguo 14/06/2018, 22:30
 
Fecha de Ingreso: diciembre-2015
Mensajes: 9
Antigüedad: 8 años, 4 meses
Puntos: 0
modificar select

buenas noches a todos,

estoy buscando ayuda con un select al cual se le a dado un estilo pero este no me modifica el elemento seleccionado.

Código HTML:
<form method="post" action="http://www.myweb.co/">
  <input name="languaje" value="es" type="hidden">
  <bdo dir="ltr" lang="es">
    <label for="lang">Lenguaje: </label>
  </bdo>
  <div name="lang" class="custom-select autosubmit" dir="ltr" id="lang" lang="es"> 
  <select name="lang" class="autosubmit" dir="ltr" id="lang" placeholder="Español" lang="es">
    <option value="pt">Português</option>
    <option value="en">English</option>
    <option value="fr">Français</option>
    <option value="it">Italiano</option>
    <option value="es" selected="selected">Español</option>
  </select>
  <div class="select-selected">Français</div>
    <div class="select-items select-hide">
    <div>Português</div>
    <div>English</div>
    <div class="same-as-selected">Français</div>
    <div>Italiano</div>
    <div>Español</div>
  </div>
</div>
</form> 

en el código anterior se selecciona el elemento deseado y este se muestra correctamente tanto en el select del html como en el select enmascarado por javascript pero resulta que no cambia el selected por default en el html

codigo Javascript

Código Javascript:
Ver original
  1. var x, i, j, selElmnt, a, b, c;
  2. /*look for any elements with the class "custom-select":*/
  3. x = document.getElementsByClassName("custom-select");
  4. for (i = 0; i < x.length; i++) {
  5.     selElmnt = x[i].getElementsByTagName("select")[0];
  6.     /*for each element, create a new DIV that will act as the selected item:*/
  7.     a = document.createElement("DIV");
  8.     a.setAttribute("class", "select-selected");
  9.     a.innerHTML = selElmnt.options[selElmnt.selectedIndex].innerHTML;
  10.     x[i].appendChild(a);
  11.     /*for each element, create a new DIV that will contain the option list:*/
  12.     b = document.createElement("DIV");
  13.     b.setAttribute("class", "select-items select-hide");
  14.     for (j = 0; j < selElmnt.length; j++) {
  15.         /*for each option in the original select element,
  16.         create a new DIV that will act as an option item:*/
  17.         c = document.createElement("DIV");
  18.         c.innerHTML = selElmnt.options[j].innerHTML;
  19.         c.addEventListener("click", function(e) {
  20.             /*when an item is clicked, update the original select box,
  21.             and the selected item:*/
  22.             var y, i, k, s, h;
  23.             s = this.parentNode.parentNode.getElementsByTagName("select")[0];
  24.             h = this.parentNode.previousSibling;
  25.             for (i = 0; i < s.length; i++) {
  26.                 if (s.options[i].innerHTML == this.innerHTML) {
  27.                     s.selectedIndex = i;
  28.                     h.innerHTML = this.innerHTML;
  29.                     y = this.parentNode.getElementsByClassName("same-as-selected");
  30.                     for (k = 0; k < y.length; k++) {
  31.                         y[k].removeAttribute("class");
  32.                     }
  33.                     this.setAttribute("class", "same-as-selected");
  34.                     break;
  35.                 }
  36.             }
  37.             h.click();
  38.         });
  39.         b.appendChild(c);
  40.     }
  41.     x[i].appendChild(b);
  42.     a.addEventListener("click", function(e) {
  43.         /*when the select box is clicked, close any other select boxes,
  44.         and open/close the current select box:*/
  45.         e.stopPropagation();
  46.         closeAllSelect(this);
  47.         this.nextSibling.classList.toggle("select-hide");
  48.         this.classList.toggle("select-arrow-active");
  49.     });
  50. }
  51.  
  52. function closeAllSelect(elmnt) {
  53.     /*a function that will close all select boxes in the document,
  54.     except the current select box:*/
  55.     var x, y, i, arrNo = [];
  56.     x = document.getElementsByClassName("select-items");
  57.     y = document.getElementsByClassName("select-selected");
  58.     for (i = 0; i < y.length; i++) {
  59.         if (elmnt == y[i]) {
  60.             arrNo.push(i)
  61.         } else {
  62.             y[i].classList.remove("select-arrow-active");
  63.         }
  64.     }
  65.     for (i = 0; i < x.length; i++) {
  66.         if (arrNo.indexOf(i)) {
  67.             x[i].classList.add("select-hide");
  68.         }
  69.     }
  70. }
  71. /*if the user clicks anywhere outside the select box,
  72. then close all select boxes:*/
  73. document.addEventListener("click", closeAllSelect);



el css

Código CSS:
Ver original
  1. /*the container must be positioned relative:*/
  2. .custom-select {
  3.   position: relative;
  4.   font-family: Arial;
  5. }
  6. .custom-select select {
  7.   // display: none; /*hide original SELECT element:*/
  8. }
  9. .select-selected {
  10.   background-color: DodgerBlue;
  11. }
  12. /*style the arrow inside the select element:*/
  13. .select-selected:after {
  14.   position: absolute;
  15.   content: "";
  16.   top: 14px;
  17.   right: 10px;
  18.   width: 0;
  19.   height: 0;
  20.   border: 6px solid transparent;
  21.   border-color: #fff transparent transparent transparent;
  22. }
  23. /*point the arrow upwards when the select box is open (active):*/
  24. .select-selected.select-arrow-active:after {
  25.   border-color: transparent transparent #fff transparent;
  26.   top: 7px;
  27. }
  28. /*style the items (options), including the selected item:*/
  29. .select-items div,.select-selected {
  30.   color: #ffffff;
  31.   padding: 8px 16px;
  32.   border: 1px solid transparent;
  33.   border-color: transparent transparent rgba(0, 0, 0, 0.1) transparent;
  34.   cursor: pointer;
  35.   user-select: none;
  36. }
  37. /*style items (options):*/
  38. .select-items {
  39.   position: absolute;
  40.   background-color: DodgerBlue;
  41.   top: 100%;
  42.   left: 0;
  43.   right: 0;
  44.   z-index: 99;
  45. }
  46. /*hide the items when the select box is closed:*/
  47. .select-hide {
  48.   display: none;
  49. }
  50. .select-items div:hover, .same-as-selected {
  51.   background-color: rgba(0, 0, 0, 0.1);
  52. }
claro que el formulario proviene de una class de php, por tanto me gustaria saber como puedo hacer que el valor seleccionado se modifique en la option del select.

agradezco su ayuda ya que e malgastado mas de una semana en este problema y me tiene desanimado.

Etiquetas: elemento, java, modificar, muestra, seleccionado, select, valor
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 05:24.