Foros del Web » Programando para Internet » Javascript »

Cómo modificar TODOS los TAGS "input" de un archivo HTML ???

Estas en el tema de Cómo modificar TODOS los TAGS "input" de un archivo HTML ??? en el foro de Javascript en Foros del Web. Hola. Quiero que al hacer click sobre TODOS los input type="text" suceda select(); y no tener que escribir en cada uno de ellos: onblur="select();" Cómo ...
  #1 (permalink)  
Antiguo 30/09/2016, 13:19
SLD
 
Fecha de Ingreso: diciembre-2013
Mensajes: 121
Antigüedad: 10 años, 4 meses
Puntos: 6
Cómo modificar TODOS los TAGS "input" de un archivo HTML ???

Hola.

Quiero que al hacer click sobre TODOS los input type="text"
suceda select();

y no tener que escribir en cada uno de ellos:

onblur="select();"


Cómo puedo hacerlo ???


Gracias.
  #2 (permalink)  
Antiguo 30/09/2016, 14:39
 
Fecha de Ingreso: noviembre-2015
Mensajes: 231
Antigüedad: 8 años, 5 meses
Puntos: 86
Respuesta: Cómo modificar TODOS los TAGS "input" de un archivo HTML ???

Es algo sencillo de hacer. Un par de nociones básicas y ya está
Código HTML:
Ver original
  1. <!DOCTYPE html>
  2. <html dir="ltr" lang="es-es">
  3.     <head>
  4.         <title></title>
  5.         <meta charset="utf-8">      
  6.     </head>
  7.     <body>
  8.  
  9.         <form>
  10.             <input type="text" value="foo">
  11.             <input type="text" value="bar">
  12.             <input type="text" value="candy">
  13.         </form>
  14.  
  15.         <script>
  16.             Array.prototype.forEach.call(document.querySelectorAll('input[type="text"]'), function(e, i, a){
  17.                 a[i].addEventListener('focus', function() {
  18.                     this.select();
  19.                 }, false);
  20.             });
  21.         </script>
  22.  
  23.     </body>
  24. </html>
  #3 (permalink)  
Antiguo 30/09/2016, 23:53
Avatar de Alexis88
Philosopher
 
Fecha de Ingreso: noviembre-2011
Ubicación: Tacna, Perú
Mensajes: 5.552
Antigüedad: 12 años, 5 meses
Puntos: 977
Respuesta: Cómo modificar TODOS los TAGS "input" de un archivo HTML ???

También puedes delegar el evento mediante la propiedad event.target:
Código Javascript:
Ver original
  1. document.addEventListener("click", function(event){
  2.     if (event.target.type == "text"){
  3.         select();
  4.     }
  5. }, false);

La condición y la instrucción también pueden acomodarse así:
Código Javascript:
Ver original
  1. event.target.type == "text" && select();

Dicha propiedad almacena al elemento afectado directamente por el evento. Cuando se comprueba que el tipo del elemento afectado por el evento es "text", se ejecuta la función "select". Si deseas, también puedes comprobar que se trate de un <input>:
Código Javascript:
Ver original
  1. event.target.tagName == "INPUT" && event.target.type == "text"

Con lo cual quedaría así:
Código Javascript:
Ver original
  1. document.addEventListener("click", function(event){
  2.     (event.target.tagName == "INPUT" && event.target.type == "text") && select();
  3. }, false);

DEMO

Esto te servirá tanto si tienes uno o N elementos como si fueron añadidos de forma síncrona (al cargar el documento) o asíncrona (añadidos dinámicamente).

__________________
«Juro por mi vida y mi amor por ella, que jamás viviré para el provecho de otro hombre, ni le pediré a otro hombre que viva para el mío».

Ayn Rand

Última edición por Alexis88; 30/09/2016 a las 23:59 Razón: DEMO
  #4 (permalink)  
Antiguo 04/10/2016, 06:03
Avatar de skywolker  
Fecha de Ingreso: julio-2011
Ubicación: España
Mensajes: 195
Antigüedad: 12 años, 8 meses
Puntos: 8
Respuesta: Cómo modificar TODOS los TAGS "input" de un archivo HTML ???

Código HTML:
$(function(){
  $("#select_all").click ( function(){
   $("#frm1 input[type='text'].child").attr ( "" , $(this).attr("" ) );
  });
});

<form id="frm1">
    <table>
        <tr>
            <td>
                <input type="text" id="select_all" />
            </td>
        </tr>
        <tr>
            <td>
                <input type="text" name="select[]" class="child" />
            </td>
        </tr>
        <tr>
            <td>
                <input type="text" name="select[]" class="child" />
            </td>
        </tr>
        <tr>
            <td>
                <input type="text" name="select[]" class="child" />
            </td>
        </tr>
    </table>
    </form> 

Etiquetas: html, input, modificar, select, tags, text
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 20:23.