{"id":250,"date":"2023-01-18T20:44:48","date_gmt":"2023-01-18T20:44:48","guid":{"rendered":"http:\/\/javigomez.org\/?p=250"},"modified":"2025-09-29T20:34:09","modified_gmt":"2025-09-29T20:34:09","slug":"anadir-y-borrar-elementos-de-un-selector-select","status":"publish","type":"post","link":"https:\/\/javigomez.org\/index.php\/2023\/01\/18\/anadir-y-borrar-elementos-de-un-selector-select\/","title":{"rendered":"A\u00f1adir y borrar elementos de un selector (select)"},"content":{"rendered":"\n<p>El siguiente c\u00f3digo muestra como se puede a\u00f1adir un elemento a un &lt;select> y como se puede borrar un elemento seleccionado.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html>\n&lt;html>\n\n&lt;head>\n    &lt;meta http-equiv=\"content-type\" content=\"text\/html;charset=utf-8\">\n    &lt;title>Manejando Selector&lt;\/title>\n\n&lt;\/head>\n\n&lt;body>\n    &lt;h1>Manejando un Selector (a\u00f1adir y quitar elementos)&lt;\/h1>\n    &lt;input type=\"text\" id=\"nombre\">\n    &lt;button onclick=\"anadir()\">a\u00f1adir&lt;\/button>\n    &lt;button onclick=\"borrar()\">borrar seleccionado&lt;\/button>\n    &lt;div id=\"borrado\">&lt;\/div>\n    &lt;p>Elementos&lt;\/p>\n\n    &lt;select class=\"select\" id=\"sel1\" size=\"5\" multiple=\"multiple\">\n        &lt;option value=\"1\">Uno&lt;\/option>\n        &lt;option value=\"2\">Dos&lt;\/option>\n        &lt;option value=\"4\">Cuatro&lt;\/option>\n        &lt;option value=\"3\">Tres&lt;\/option>\n        &lt;option value=\"5\">Cinco&lt;\/option>\n    &lt;\/select>\n\n\n    &lt;script type=\"text\/javascript\">\n\n        function anadir() {\n            var nombre=document.getElementById(\"nombre\").value;\n\n            var select = document.getElementById(\"sel1\");\n            var num=select.children.length;\n            var opt=document.createElement(\"option\");\n            opt.value=num;\n            var txtOpt=document.createTextNode(nombre);\n            opt.appendChild(txtOpt);\n            select.append(opt);\n        }\n\n        function borrar() {\n\n            var select = document.getElementById(\"sel1\");\n            var elementos=select.options.length; \/\/   var elementos=select.children.length;\n            for (i=0;i&lt;elementos;i++){\n                if (select.options&#91;i].selected) {\n                    var opt=select.options&#91;i];\n                    var valueOpt=opt.value;\n                    var txtOpt=opt.text;\n                    document.getElementById(\"borrado\").innerHTML=\"Elemento borrado. Value: \" + valueOpt + \" Texto: \" + txtOpt;\n                    opt.parentElement.removeChild(opt);\n                    i=elementos;\n                }\n            }\n\n        }\n    &lt;\/script>\n&lt;\/body>\n\n&lt;\/html><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>El siguiente c\u00f3digo muestra como se puede a\u00f1adir un elemento a un &lt;select> y como se puede borrar un elemento seleccionado.<\/p>\n","protected":false},"author":1,"featured_media":251,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[36,37,103,104,205,286,301],"class_list":["post-250","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","tag-append","tag-appendchild","tag-createelement","tag-createtextnode","tag-javascritp","tag-removechild","tag-select"],"_links":{"self":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/250","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/comments?post=250"}],"version-history":[{"count":1,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/250\/revisions"}],"predecessor-version":[{"id":954,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/250\/revisions\/954"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/media\/251"}],"wp:attachment":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/media?parent=250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/categories?post=250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/tags?post=250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}