{"id":657,"date":"2014-09-19T19:44:23","date_gmt":"2014-09-19T19:44:23","guid":{"rendered":"http:\/\/luisrodriguez.pe\/blog\/?p=657"},"modified":"2014-09-19T19:51:27","modified_gmt":"2014-09-19T19:51:27","slug":"crear-un-autocompletar-en-jquery","status":"publish","type":"post","link":"https:\/\/luisrodriguez.pe\/blog\/crear-un-autocompletar-en-jquery\/","title":{"rendered":"Crear un autocompletar en jquery"},"content":{"rendered":"<p>En ocasi\u00f3n tenemos mucha data que cargar en un select que nos inclinamos por usar un autocompletar y de hecho podemos encontrar varios pero no siempre se amoldan a nuestras necesidades y modificarlos puede llegar a dar dolor de cabeza.<\/p>\n<p>Para los reacios que no quieren ver las ventajas aqu\u00ed les dejo unas.<\/p>\n<ol>\n<li>Velocidad para mejorar tu SEO<\/li>\n<li>Facilidad de B\u00fasqueda UX<\/li>\n<li>Dise\u00f1o m\u00e1s atractivo de UI<\/li>\n<\/ol>\n<p>A continuaci\u00f3n voy hacer un ejemplo paso a paso de c\u00f3mo se crea uno en jquery y php.<\/p>\n<p>aqui el html con lineas comentadas explicando<br \/>\n<code><\/p>\n\n<div class=\"wp_syntax\"><table><tr><td class=\"code\"><pre class=\"html\" style=\"font-family:monospace;\">&nbsp;\n&lt;!DOCTYPE html&gt; \r\n&lt;html&gt;\r\n    &lt;head&gt;\r\n        &lt;title&gt;atocompletar&lt;\/title&gt;\r\n        &lt;meta charset=&quot;UTF-8&quot;&gt;\r\n        &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;\r\n    &lt;\/head&gt;\r\n    &lt;body&gt;\r\n        &lt;!--formulario del autocompletar--&gt;\r\n        &lt;form method=&quot;post&quot;&gt;\r\n            &lt;!--aqui escribimos el nombre de departamento y activa el autocompletar--&gt;\r\n            &lt;input type=&quot;text&quot; name=&quot;departamentos&quot; id=&quot;departamentos&quot; \/&gt;\r\n            &lt;!--aqui va el codigo del departamento cuando selecciona--&gt;\r\n            &lt;input type=&quot;hidden&quot; name=&quot;codigo_departamentos&quot; id=&quot;codigo_departamentos&quot; \/&gt;\r\n        &lt;\/form&gt; \r\n        &lt;!--agregamos jquery--&gt;\r\n        &lt;script src=&quot;js\/jquery.js&quot; type=&quot;text\/javascript&quot;&gt;&lt;\/script&gt;\r\n        &lt;!--agregamos nuestro script de autocompletar--&gt;\r\n        &lt;script src=&quot;js\/autocompletar.js&quot; type=&quot;text\/javascript&quot;&gt;&lt;\/script&gt;\r\n    &lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre><\/td><\/tr><\/table><\/div>\n\n<p><\/code><\/p>\n<p>un php que debe mandar un json con la data real pero aqui puse un dato en duro pero en javascript hago que envie un dato en get para que sirva para filtrar una consulta sql que eso seria en casos reales. <\/p>\n<p>solo por demo coloco un json en duro.<br \/>\n<code><\/p>\n\n<div class=\"wp_syntax\"><table><tr><td class=\"code\"><pre class=\"php\" style=\"font-family:monospace;\"><span style=\"color: #000088;\">$data<\/span><span style=\"color: #339933;\">=<\/span><span style=\"color: #990000;\">array<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #990000;\">array<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000ff;\">&quot;codigo&quot;<\/span><span style=\"color: #339933;\">=&gt;<\/span><span style=\"color: #0000ff;\">&quot;001&quot;<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">&quot;provincia&quot;<\/span><span style=\"color: #339933;\">=&gt;<\/span><span style=\"color: #0000ff;\">&quot;LIMA&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">,<\/span>\n            <span style=\"color: #990000;\">array<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000ff;\">&quot;codigo&quot;<\/span><span style=\"color: #339933;\">=&gt;<\/span><span style=\"color: #0000ff;\">&quot;002&quot;<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">&quot;provincia&quot;<\/span><span style=\"color: #339933;\">=&gt;<\/span><span style=\"color: #0000ff;\">&quot;AYACUCHO&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">,<\/span>\n            <span style=\"color: #990000;\">array<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000ff;\">&quot;codigo&quot;<\/span><span style=\"color: #339933;\">=&gt;<\/span><span style=\"color: #0000ff;\">&quot;003&quot;<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">&quot;provincia&quot;<\/span><span style=\"color: #339933;\">=&gt;<\/span><span style=\"color: #0000ff;\">&quot;JUNIN&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">,<\/span>\n            <span style=\"color: #990000;\">array<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000ff;\">&quot;codigo&quot;<\/span><span style=\"color: #339933;\">=&gt;<\/span><span style=\"color: #0000ff;\">&quot;004&quot;<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">&quot;provincia&quot;<\/span><span style=\"color: #339933;\">=&gt;<\/span><span style=\"color: #0000ff;\">&quot;PUNO&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">,<\/span>\n            <span style=\"color: #990000;\">array<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000ff;\">&quot;codigo&quot;<\/span><span style=\"color: #339933;\">=&gt;<\/span><span style=\"color: #0000ff;\">&quot;005&quot;<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">&quot;provincia&quot;<\/span><span style=\"color: #339933;\">=&gt;<\/span><span style=\"color: #0000ff;\">&quot;TRUJILLO&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">,<\/span>\n            <span style=\"color: #990000;\">array<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000ff;\">&quot;codigo&quot;<\/span><span style=\"color: #339933;\">=&gt;<\/span><span style=\"color: #0000ff;\">&quot;006&quot;<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">&quot;provincia&quot;<\/span><span style=\"color: #339933;\">=&gt;<\/span><span style=\"color: #0000ff;\">&quot;IQUITOS&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">,<\/span>\n            <span style=\"color: #990000;\">array<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000ff;\">&quot;codigo&quot;<\/span><span style=\"color: #339933;\">=&gt;<\/span><span style=\"color: #0000ff;\">&quot;007&quot;<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">&quot;provincia&quot;<\/span><span style=\"color: #339933;\">=&gt;<\/span><span style=\"color: #0000ff;\">&quot;LORETO&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span>\n        <span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n&nbsp;\n<span style=\"color: #b1b100;\">echo<\/span> <span style=\"color: #990000;\">json_encode<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #000088;\">$data<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span><\/pre><\/td><\/tr><\/table><\/div>\n\n<p><\/code><\/p>\n<p>las siguientes lineas son la base de todo y cada linea tiene explicacion<\/p>\n<p><code><\/p>\n\n<div class=\"wp_syntax\"><table><tr><td class=\"code\"><pre class=\"javascript\" style=\"font-family:monospace;\"><span style=\"color: #006600; font-style: italic;\">\/\/cuando tecle en el input se ejecutara<\/span>\n    $<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #3366CC;\">&quot;#departamentos&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span>.<span style=\"color: #660066;\">on<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #3366CC;\">&quot;keyup&quot;<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #000066; font-weight: bold;\">function<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#123;<\/span>\n        <span style=\"color: #006600; font-style: italic;\">\/\/la referencia al input lo metemos en una variable<\/span>\n        <span style=\"color: #000066; font-weight: bold;\">var<\/span> $this<span style=\"color: #339933;\">=<\/span>$<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #000066; font-weight: bold;\">this<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n        <span style=\"color: #006600; font-style: italic;\">\/\/eliminamos el div si existiera<\/span>\n        $<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #3366CC;\">&quot;#autocompletar&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span>.<span style=\"color: #660066;\">remove<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>  \n        <span style=\"color: #006600; font-style: italic;\">\/\/solo si es mayor a 3 envia un ajax<\/span>\n        <span style=\"color: #000066; font-weight: bold;\">if<\/span> <span style=\"color: #009900;\">&#40;<\/span>$<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #3366CC;\">&quot;#departamentos&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span>.<span style=\"color: #660066;\">val<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span>.<span style=\"color: #660066;\">length<\/span> <span style=\"color: #339933;\">&gt;=<\/span><span style=\"color: #CC0000;\">3<\/span><span style=\"color: #009900;\">&#41;<\/span>\n        <span style=\"color: #009900;\">&#123;<\/span>\n            <span style=\"color: #006600; font-style: italic;\">\/\/para hacer bonito el efecto agregamos un gif de load en la parte derecha del input <\/span>\n            <span style=\"color: #006600; font-style: italic;\">\/\/sera un background<\/span>\n            $this.<span style=\"color: #660066;\">css<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#123;<\/span><span style=\"color: #3366CC;\">&quot;background&quot;<\/span><span style=\"color: #339933;\">:<\/span><span style=\"color: #3366CC;\">&quot;url('chrome-extension:\/\/hlngmmdolgbdnnimbmblfhhndibdipaf\/images\/loader.gif') top right no-repeat&quot;<\/span><span style=\"color: #009900;\">&#125;<\/span><span style=\"color: #009900;\">&#41;<\/span>\n           <span style=\"color: #006600; font-style: italic;\">\/\/capturamos el valor de ingreso en el campo<\/span>\n            <span style=\"color: #000066; font-weight: bold;\">var<\/span> departamentos<span style=\"color: #339933;\">=<\/span>$this.<span style=\"color: #660066;\">val<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n            <span style=\"color: #006600; font-style: italic;\">\/\/hacemos un ajax en get y enviamos un variable get que es el dato que ingreso en el input<\/span>\n            $.<span style=\"color: #000066; font-weight: bold;\">get<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #3366CC;\">&quot;data.php&quot;<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #009900;\">&#123;<\/span><span style=\"color: #3366CC;\">&quot;departamentos&quot;<\/span><span style=\"color: #339933;\">:<\/span>departamentos<span style=\"color: #009900;\">&#125;<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #000066; font-weight: bold;\">function<\/span><span style=\"color: #009900;\">&#40;<\/span>data<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#123;<\/span>\n                <span style=\"color: #006600; font-style: italic;\">\/\/ahora crearemos un div para mostrar los resultados pero queremos que sea debajo del input <\/span>\n                <span style=\"color: #006600; font-style: italic;\">\/\/y para eso debemos obtener la posicion del elemento para eso position de jquery<\/span>\n                posicion<span style=\"color: #339933;\">=<\/span>$this.<span style=\"color: #660066;\">position<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span> \n                <span style=\"color: #006600; font-style: italic;\">\/\/guardamos el top , left y width <\/span>\n                <span style=\"color: #006600; font-style: italic;\">\/\/<\/span>\n                <span style=\"color: #006600; font-style: italic;\">\/\/el top actual + el alto de la cajita para posicionarlo debajo de la cajita me refiero al input<\/span>\n                <span style=\"color: #006600; font-style: italic;\">\/\/adicionalmente 10 px mas para que no sea tan pegado<\/span>\n                <span style=\"color: #000066; font-weight: bold;\">var<\/span> top<span style=\"color: #339933;\">=<\/span>parseInt<span style=\"color: #009900;\">&#40;<\/span>posicion.<span style=\"color: #660066;\">top<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">+<\/span>parseInt<span style=\"color: #009900;\">&#40;<\/span>$this.<span style=\"color: #660066;\">height<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">+<\/span><span style=\"color: #CC0000;\">10<\/span><span style=\"color: #339933;\">;<\/span> \n                <span style=\"color: #006600; font-style: italic;\">\/\/el left del la cajita<\/span>\n                <span style=\"color: #000066; font-weight: bold;\">var<\/span> left<span style=\"color: #339933;\">=<\/span>posicion.<span style=\"color: #660066;\">left<\/span><span style=\"color: #339933;\">;<\/span>\n                <span style=\"color: #006600; font-style: italic;\">\/\/el ancho de la cajita + 100 px<\/span>\n                <span style=\"color: #000066; font-weight: bold;\">var<\/span> width<span style=\"color: #339933;\">=<\/span>parseInt<span style=\"color: #009900;\">&#40;<\/span>$this.<span style=\"color: #660066;\">width<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">+<\/span><span style=\"color: #CC0000;\">100<\/span>\n                <span style=\"color: #006600; font-style: italic;\">\/\/debemos crear el div pero consultamos si existe para no volver a crearlo<\/span>\n                <span style=\"color: #000066; font-weight: bold;\">if<\/span> <span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #339933;\">!<\/span>$<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #3366CC;\">&quot;#autocompletar&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span>.<span style=\"color: #660066;\">length<\/span><span style=\"color: #009900;\">&#41;<\/span>\n                <span style=\"color: #009900;\">&#123;<\/span>\n                    <span style=\"color: #006600; font-style: italic;\">\/\/en caso no exista lo creamos dando las medidas<\/span>\n                    $<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #3366CC;\">&quot;body&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span>.<span style=\"color: #660066;\">append<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #3366CC;\">&quot;&lt;div id='autocompletar' style='overflow:auto;top:&quot;<\/span><span style=\"color: #339933;\">+<\/span>top<span style=\"color: #339933;\">+<\/span><span style=\"color: #3366CC;\">&quot;px;left:&quot;<\/span><span style=\"color: #339933;\">+<\/span>left<span style=\"color: #339933;\">+<\/span><span style=\"color: #3366CC;\">&quot;px;width:&quot;<\/span><span style=\"color: #339933;\">+<\/span>width<span style=\"color: #339933;\">+<\/span><span style=\"color: #3366CC;\">&quot;px; height:100px;position:absolute; border:1px solid #ebebeb'&gt;&lt;\/div&gt;&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span>\n                <span style=\"color: #009900;\">&#125;<\/span>\n                <span style=\"color: #000066; font-weight: bold;\">var<\/span> html<span style=\"color: #339933;\">=<\/span><span style=\"color: #3366CC;\">''<\/span><span style=\"color: #339933;\">;<\/span>\n                <span style=\"color: #006600; font-style: italic;\">\/\/ahora creamos el contenido que va dentro del div sera un ul con la respuesta<\/span>\n                html <span style=\"color: #339933;\">+=<\/span><span style=\"color: #3366CC;\">'&lt;ul id=&quot;lista&quot;&gt;'<\/span><span style=\"color: #339933;\">;<\/span>\n                data<span style=\"color: #339933;\">=<\/span>$.<span style=\"color: #660066;\">parseJSON<\/span><span style=\"color: #009900;\">&#40;<\/span>data<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n                <span style=\"color: #006600; font-style: italic;\">\/\/un recorrido a la respuesta que nos envio el ajax que en este caso se llama data<\/span>\n                $.<span style=\"color: #660066;\">each<\/span><span style=\"color: #009900;\">&#40;<\/span>data<span style=\"color: #339933;\">,<\/span> <span style=\"color: #000066; font-weight: bold;\">function<\/span><span style=\"color: #009900;\">&#40;<\/span>i<span style=\"color: #339933;\">,<\/span> item<span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #009900;\">&#123;<\/span>\n                        <span style=\"color: #006600; font-style: italic;\">\/\/creamos un li por cada vuelta y con atributos en el li que <\/span>\n                        <span style=\"color: #006600; font-style: italic;\">\/\/nos serviran para que cuando seleccione guardarlos de alguna forma<\/span>\n                        html <span style=\"color: #339933;\">+=<\/span><span style=\"color: #3366CC;\">'&lt;li data-codigo=&quot;'<\/span><span style=\"color: #339933;\">+<\/span>item.<span style=\"color: #660066;\">codigo<\/span><span style=\"color: #339933;\">+<\/span><span style=\"color: #3366CC;\">'&quot;  data-provincia=&quot;'<\/span><span style=\"color: #339933;\">+<\/span>item.<span style=\"color: #660066;\">provincia<\/span><span style=\"color: #339933;\">+<\/span><span style=\"color: #3366CC;\">'&quot;&gt;&lt;a href=&quot;javascript:;&quot;&gt;'<\/span><span style=\"color: #339933;\">+<\/span>item.<span style=\"color: #660066;\">provincia<\/span><span style=\"color: #339933;\">+<\/span><span style=\"color: #3366CC;\">'&lt;\/a&gt;&lt;\/li&gt;'<\/span><span style=\"color: #339933;\">;<\/span>\n                <span style=\"color: #009900;\">&#125;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n                html <span style=\"color: #339933;\">+=<\/span><span style=\"color: #3366CC;\">'&lt;\/ul&gt;'<\/span><span style=\"color: #339933;\">;<\/span>\n                <span style=\"color: #006600; font-style: italic;\">\/\/cancelamos el efecto del load del gif<\/span>\n                $this.<span style=\"color: #660066;\">css<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#123;<\/span><span style=\"color: #3366CC;\">&quot;background&quot;<\/span><span style=\"color: #339933;\">:<\/span><span style=\"color: #3366CC;\">&quot;&quot;<\/span><span style=\"color: #009900;\">&#125;<\/span><span style=\"color: #009900;\">&#41;<\/span>\n                <span style=\"color: #006600; font-style: italic;\">\/\/limpiamos todo lo que pueda tener el div contenedor y agregamos toda nuestra ul que tiene<\/span>\n                <span style=\"color: #006600; font-style: italic;\">\/\/la data obtenida como respuesta del ajax<\/span>\n                $<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #3366CC;\">&quot;#autocompletar&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span>.<span style=\"color: #660066;\">empty<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span>.<span style=\"color: #660066;\">append<\/span><span style=\"color: #009900;\">&#40;<\/span>html<span style=\"color: #009900;\">&#41;<\/span>\n            <span style=\"color: #009900;\">&#125;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n        <span style=\"color: #009900;\">&#125;<\/span>\n    <span style=\"color: #009900;\">&#125;<\/span><span style=\"color: #009900;\">&#41;<\/span><\/pre><\/td><\/tr><\/table><\/div>\n\n<p><\/code><\/p>\n<p>ahora la funcion que se ejecuta al selecciona un item de la respuesta.<\/p>\n<p><code><\/p>\n\n<div class=\"wp_syntax\"><table><tr><td class=\"code\"><pre class=\"javascript\" style=\"font-family:monospace;\"><span style=\"color: #006600; font-style: italic;\">\/\/funcion que se ejecuta cuando hace click en una opcion de la respuesta al escribir<\/span>\n    $<span style=\"color: #009900;\">&#40;<\/span>document<span style=\"color: #009900;\">&#41;<\/span>.<span style=\"color: #660066;\">on<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #3366CC;\">&quot;click&quot;<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #3366CC;\">&quot;li&quot;<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #000066; font-weight: bold;\">function<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#123;<\/span>\n        <span style=\"color: #006600; font-style: italic;\">\/\/aqui tenemos los atributos que creamos en el li<\/span>\n        <span style=\"color: #006600; font-style: italic;\">\/\/jalamos el codigo y el texto del nombre de la provincia<\/span>\n        codigo<span style=\"color: #339933;\">=<\/span>$<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #000066; font-weight: bold;\">this<\/span><span style=\"color: #009900;\">&#41;<\/span>.<span style=\"color: #660066;\">attr<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #3366CC;\">&quot;data-codigo&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n        provincia<span style=\"color: #339933;\">=<\/span>$<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #000066; font-weight: bold;\">this<\/span><span style=\"color: #009900;\">&#41;<\/span>.<span style=\"color: #660066;\">attr<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #3366CC;\">&quot;data-provincia&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n        <span style=\"color: #006600; font-style: italic;\">\/\/asignamos los valores<\/span>\n        $<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #3366CC;\">&quot;#codigo_departamentos&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span>.<span style=\"color: #660066;\">val<\/span><span style=\"color: #009900;\">&#40;<\/span>codigo<span style=\"color: #009900;\">&#41;<\/span>\n        $<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #3366CC;\">&quot;#departamentos&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span>.<span style=\"color: #660066;\">val<\/span><span style=\"color: #009900;\">&#40;<\/span>provincia<span style=\"color: #009900;\">&#41;<\/span>\n        <span style=\"color: #006600; font-style: italic;\">\/\/y finalmente eliminamos el elementos del autocompletar<\/span>\n        $<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #3366CC;\">&quot;#autocompletar&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span>.<span style=\"color: #660066;\">remove<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n    <span style=\"color: #009900;\">&#125;<\/span><span style=\"color: #009900;\">&#41;<\/span><\/pre><\/td><\/tr><\/table><\/div>\n\n<p><\/code><\/p>\n<p>puede ver en funcionamiento el script<\/p>\n<p><a href=\"http:\/\/luisrodriguez.pe\/demos\/scripts\/autocompletar\/\" target=\"_blank\">autocompletar jquery demo<\/a><br \/>\ny descargarlo aqui<br \/>\n<a href=\"http:\/\/luisrodriguez.pe\/demos\/scripts\/autocompletar\/autocompletar.zip\" target=\"_blank\">autocompletar jquery<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"En ocasi\u00f3n tenemos mucha data que cargar en un select que nos inclinamos por usar un autocompletar y de hecho podemos encontrar varios pero no siempre se amoldan a nuestras necesidades y modificarlos puede llegar a dar dolor de cabeza. Para los reacios que no quieren ver las ventajas aqu\u00ed les dejo unas. Velocidad para[&#8230;]","protected":false},"author":1,"featured_media":991,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,14],"tags":[63,22,17],"class_list":["post-657","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","category-jquery","tag-autocompletar","tag-javascript-2","tag-jquery-2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Crear un autocompletar en jquery - Blog Luis Rodriguez<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/luisrodriguez.pe\/blog\/crear-un-autocompletar-en-jquery\/\" \/>\n<meta property=\"og:locale\" content=\"es_ES\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Crear un autocompletar en jquery - Blog Luis Rodriguez\" \/>\n<meta property=\"og:description\" content=\"En ocasi\u00f3n tenemos mucha data que cargar en un select que nos inclinamos por usar un autocompletar y de hecho podemos encontrar varios pero no siempre se amoldan a nuestras necesidades y modificarlos puede llegar a dar dolor de cabeza. Para los reacios que no quieren ver las ventajas aqu\u00ed les dejo unas. Velocidad para[...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/luisrodriguez.pe\/blog\/crear-un-autocompletar-en-jquery\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog Luis Rodriguez\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/luisrodriguez.pe\" \/>\n<meta property=\"article:published_time\" content=\"2014-09-19T19:44:23+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2014-09-19T19:51:27+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/luisrodriguez.pe\/blog\/wp-content\/uploads\/2014\/09\/Screenshot_2.png\" \/>\n\t<meta property=\"og:image:width\" content=\"438\" \/>\n\t<meta property=\"og:image:height\" content=\"309\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Luis Rodriguez\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@luizmirodriguez\" \/>\n<meta name=\"twitter:site\" content=\"@luizmirodriguez\" \/>\n<meta name=\"twitter:label1\" content=\"Escrito por\" \/>\n\t<meta name=\"twitter:data1\" content=\"Luis Rodriguez\" \/>\n\t<meta name=\"twitter:label2\" content=\"Tiempo de lectura\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutos\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/luisrodriguez.pe\/blog\/crear-un-autocompletar-en-jquery\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/luisrodriguez.pe\/blog\/crear-un-autocompletar-en-jquery\/\"},\"author\":{\"name\":\"Luis Rodriguez\",\"@id\":\"https:\/\/luisrodriguez.pe\/blog\/#\/schema\/person\/ec55de81e01fdaf3533eb78f6a49090c\"},\"headline\":\"Crear un autocompletar en jquery\",\"datePublished\":\"2014-09-19T19:44:23+00:00\",\"dateModified\":\"2014-09-19T19:51:27+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/luisrodriguez.pe\/blog\/crear-un-autocompletar-en-jquery\/\"},\"wordCount\":193,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/luisrodriguez.pe\/blog\/crear-un-autocompletar-en-jquery\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/luisrodriguez.pe\/blog\/wp-content\/uploads\/2014\/09\/Screenshot_2.png\",\"keywords\":[\"autocompletar\",\"javascript\",\"jquery\"],\"articleSection\":[\"JAVASCRIPT\",\"Jquery\"],\"inLanguage\":\"es\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/luisrodriguez.pe\/blog\/crear-un-autocompletar-en-jquery\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/luisrodriguez.pe\/blog\/crear-un-autocompletar-en-jquery\/\",\"url\":\"https:\/\/luisrodriguez.pe\/blog\/crear-un-autocompletar-en-jquery\/\",\"name\":\"Crear un autocompletar en jquery - Blog Luis Rodriguez\",\"isPartOf\":{\"@id\":\"https:\/\/luisrodriguez.pe\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/luisrodriguez.pe\/blog\/crear-un-autocompletar-en-jquery\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/luisrodriguez.pe\/blog\/crear-un-autocompletar-en-jquery\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/luisrodriguez.pe\/blog\/wp-content\/uploads\/2014\/09\/Screenshot_2.png\",\"datePublished\":\"2014-09-19T19:44:23+00:00\",\"dateModified\":\"2014-09-19T19:51:27+00:00\",\"author\":{\"@id\":\"https:\/\/luisrodriguez.pe\/blog\/#\/schema\/person\/ec55de81e01fdaf3533eb78f6a49090c\"},\"breadcrumb\":{\"@id\":\"https:\/\/luisrodriguez.pe\/blog\/crear-un-autocompletar-en-jquery\/#breadcrumb\"},\"inLanguage\":\"es\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/luisrodriguez.pe\/blog\/crear-un-autocompletar-en-jquery\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"es\",\"@id\":\"https:\/\/luisrodriguez.pe\/blog\/crear-un-autocompletar-en-jquery\/#primaryimage\",\"url\":\"https:\/\/luisrodriguez.pe\/blog\/wp-content\/uploads\/2014\/09\/Screenshot_2.png\",\"contentUrl\":\"https:\/\/luisrodriguez.pe\/blog\/wp-content\/uploads\/2014\/09\/Screenshot_2.png\",\"width\":438,\"height\":309},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/luisrodriguez.pe\/blog\/crear-un-autocompletar-en-jquery\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Portada\",\"item\":\"https:\/\/luisrodriguez.pe\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Crear un autocompletar en jquery\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/luisrodriguez.pe\/blog\/#website\",\"url\":\"https:\/\/luisrodriguez.pe\/blog\/\",\"name\":\"Blog Luis Rodriguez\",\"description\":\"Analytics Developer\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/luisrodriguez.pe\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"es\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/luisrodriguez.pe\/blog\/#\/schema\/person\/ec55de81e01fdaf3533eb78f6a49090c\",\"name\":\"Luis Rodriguez\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"es\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/3b0f9734b267ecb38e1860734b4caebda4efa8be9a2bb536862b6a2ba23afa82?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/3b0f9734b267ecb38e1860734b4caebda4efa8be9a2bb536862b6a2ba23afa82?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/3b0f9734b267ecb38e1860734b4caebda4efa8be9a2bb536862b6a2ba23afa82?s=96&d=mm&r=g\",\"caption\":\"Luis Rodriguez\"},\"description\":\"Hola, Soy Luis Rodriguez, radico en Lima \u2013 Per\u00fa, formado en una carrera inform\u00e1tica, desempe\u00f1ando desde el 2007 como programador web y viendo proyectos desde Front End y Back End a lo que me perfile como Full Stack. Esta Experiencia junto a mi inter\u00e9s en la Anal\u00edtica Web me ayud\u00f3 mucho entender el funcionamiento interno de las herramientas de anal\u00edtica, con las que vengo trabajo activamente desde 2017. Linkedin\",\"sameAs\":[\"http:\/\/luisrodriguez.pe\"],\"url\":\"https:\/\/luisrodriguez.pe\/blog\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Crear un autocompletar en jquery - Blog Luis Rodriguez","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/luisrodriguez.pe\/blog\/crear-un-autocompletar-en-jquery\/","og_locale":"es_ES","og_type":"article","og_title":"Crear un autocompletar en jquery - Blog Luis Rodriguez","og_description":"En ocasi\u00f3n tenemos mucha data que cargar en un select que nos inclinamos por usar un autocompletar y de hecho podemos encontrar varios pero no siempre se amoldan a nuestras necesidades y modificarlos puede llegar a dar dolor de cabeza. Para los reacios que no quieren ver las ventajas aqu\u00ed les dejo unas. Velocidad para[...]","og_url":"https:\/\/luisrodriguez.pe\/blog\/crear-un-autocompletar-en-jquery\/","og_site_name":"Blog Luis Rodriguez","article_publisher":"https:\/\/www.facebook.com\/luisrodriguez.pe","article_published_time":"2014-09-19T19:44:23+00:00","article_modified_time":"2014-09-19T19:51:27+00:00","og_image":[{"width":438,"height":309,"url":"https:\/\/luisrodriguez.pe\/blog\/wp-content\/uploads\/2014\/09\/Screenshot_2.png","type":"image\/png"}],"author":"Luis Rodriguez","twitter_card":"summary_large_image","twitter_creator":"@luizmirodriguez","twitter_site":"@luizmirodriguez","twitter_misc":{"Escrito por":"Luis Rodriguez","Tiempo de lectura":"3 minutos"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/luisrodriguez.pe\/blog\/crear-un-autocompletar-en-jquery\/#article","isPartOf":{"@id":"https:\/\/luisrodriguez.pe\/blog\/crear-un-autocompletar-en-jquery\/"},"author":{"name":"Luis Rodriguez","@id":"https:\/\/luisrodriguez.pe\/blog\/#\/schema\/person\/ec55de81e01fdaf3533eb78f6a49090c"},"headline":"Crear un autocompletar en jquery","datePublished":"2014-09-19T19:44:23+00:00","dateModified":"2014-09-19T19:51:27+00:00","mainEntityOfPage":{"@id":"https:\/\/luisrodriguez.pe\/blog\/crear-un-autocompletar-en-jquery\/"},"wordCount":193,"commentCount":0,"image":{"@id":"https:\/\/luisrodriguez.pe\/blog\/crear-un-autocompletar-en-jquery\/#primaryimage"},"thumbnailUrl":"https:\/\/luisrodriguez.pe\/blog\/wp-content\/uploads\/2014\/09\/Screenshot_2.png","keywords":["autocompletar","javascript","jquery"],"articleSection":["JAVASCRIPT","Jquery"],"inLanguage":"es","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/luisrodriguez.pe\/blog\/crear-un-autocompletar-en-jquery\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/luisrodriguez.pe\/blog\/crear-un-autocompletar-en-jquery\/","url":"https:\/\/luisrodriguez.pe\/blog\/crear-un-autocompletar-en-jquery\/","name":"Crear un autocompletar en jquery - Blog Luis Rodriguez","isPartOf":{"@id":"https:\/\/luisrodriguez.pe\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/luisrodriguez.pe\/blog\/crear-un-autocompletar-en-jquery\/#primaryimage"},"image":{"@id":"https:\/\/luisrodriguez.pe\/blog\/crear-un-autocompletar-en-jquery\/#primaryimage"},"thumbnailUrl":"https:\/\/luisrodriguez.pe\/blog\/wp-content\/uploads\/2014\/09\/Screenshot_2.png","datePublished":"2014-09-19T19:44:23+00:00","dateModified":"2014-09-19T19:51:27+00:00","author":{"@id":"https:\/\/luisrodriguez.pe\/blog\/#\/schema\/person\/ec55de81e01fdaf3533eb78f6a49090c"},"breadcrumb":{"@id":"https:\/\/luisrodriguez.pe\/blog\/crear-un-autocompletar-en-jquery\/#breadcrumb"},"inLanguage":"es","potentialAction":[{"@type":"ReadAction","target":["https:\/\/luisrodriguez.pe\/blog\/crear-un-autocompletar-en-jquery\/"]}]},{"@type":"ImageObject","inLanguage":"es","@id":"https:\/\/luisrodriguez.pe\/blog\/crear-un-autocompletar-en-jquery\/#primaryimage","url":"https:\/\/luisrodriguez.pe\/blog\/wp-content\/uploads\/2014\/09\/Screenshot_2.png","contentUrl":"https:\/\/luisrodriguez.pe\/blog\/wp-content\/uploads\/2014\/09\/Screenshot_2.png","width":438,"height":309},{"@type":"BreadcrumbList","@id":"https:\/\/luisrodriguez.pe\/blog\/crear-un-autocompletar-en-jquery\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Portada","item":"https:\/\/luisrodriguez.pe\/blog\/"},{"@type":"ListItem","position":2,"name":"Crear un autocompletar en jquery"}]},{"@type":"WebSite","@id":"https:\/\/luisrodriguez.pe\/blog\/#website","url":"https:\/\/luisrodriguez.pe\/blog\/","name":"Blog Luis Rodriguez","description":"Analytics Developer","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/luisrodriguez.pe\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"es"},{"@type":"Person","@id":"https:\/\/luisrodriguez.pe\/blog\/#\/schema\/person\/ec55de81e01fdaf3533eb78f6a49090c","name":"Luis Rodriguez","image":{"@type":"ImageObject","inLanguage":"es","@id":"https:\/\/secure.gravatar.com\/avatar\/3b0f9734b267ecb38e1860734b4caebda4efa8be9a2bb536862b6a2ba23afa82?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3b0f9734b267ecb38e1860734b4caebda4efa8be9a2bb536862b6a2ba23afa82?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3b0f9734b267ecb38e1860734b4caebda4efa8be9a2bb536862b6a2ba23afa82?s=96&d=mm&r=g","caption":"Luis Rodriguez"},"description":"Hola, Soy Luis Rodriguez, radico en Lima \u2013 Per\u00fa, formado en una carrera inform\u00e1tica, desempe\u00f1ando desde el 2007 como programador web y viendo proyectos desde Front End y Back End a lo que me perfile como Full Stack. Esta Experiencia junto a mi inter\u00e9s en la Anal\u00edtica Web me ayud\u00f3 mucho entender el funcionamiento interno de las herramientas de anal\u00edtica, con las que vengo trabajo activamente desde 2017. Linkedin","sameAs":["http:\/\/luisrodriguez.pe"],"url":"https:\/\/luisrodriguez.pe\/blog\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/luisrodriguez.pe\/blog\/wp-json\/wp\/v2\/posts\/657","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/luisrodriguez.pe\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/luisrodriguez.pe\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/luisrodriguez.pe\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/luisrodriguez.pe\/blog\/wp-json\/wp\/v2\/comments?post=657"}],"version-history":[{"count":11,"href":"https:\/\/luisrodriguez.pe\/blog\/wp-json\/wp\/v2\/posts\/657\/revisions"}],"predecessor-version":[{"id":668,"href":"https:\/\/luisrodriguez.pe\/blog\/wp-json\/wp\/v2\/posts\/657\/revisions\/668"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/luisrodriguez.pe\/blog\/wp-json\/wp\/v2\/media\/991"}],"wp:attachment":[{"href":"https:\/\/luisrodriguez.pe\/blog\/wp-json\/wp\/v2\/media?parent=657"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/luisrodriguez.pe\/blog\/wp-json\/wp\/v2\/categories?post=657"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/luisrodriguez.pe\/blog\/wp-json\/wp\/v2\/tags?post=657"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}