{"id":29269,"date":"2023-01-06T15:33:51","date_gmt":"2023-01-06T10:03:51","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-dynamically-add-an-input-to-an-html-form-closed\/"},"modified":"2023-01-06T15:33:51","modified_gmt":"2023-01-06T10:03:51","slug":"solved-dynamically-add-an-input-to-an-html-form-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-dynamically-add-an-input-to-an-html-form-closed\/","title":{"rendered":"[Solved] Dynamically add an input to an HTML form [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-6313364\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"6313364\" data-parentid=\"6313284\" data-score=\"8\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>I&#8217;m struggling whether to vote to close this as a duplicate, even though it&#8217;s not <em>exact<\/em>.  But what you&#8217;re looking for appears to be answered here.  The main difference is that where they are binding to the <code>change<\/code> event for a <code>select<\/code> element:<\/p>\n<pre><code>$(\"#selectBox\").change(function() {\n<\/code><\/pre>\n<p>you would instead bind to the <code>click<\/code> event of an anchor element (your &#8220;Add&#8221; link):<\/p>\n<pre><code>$(\"#addLink\").click(function() {\n<\/code><\/pre>\n<p>The &#8220;Delete&#8221; link would work in a similar manner.  As the HTML is created for the new input, you&#8217;d also create the &#8220;Delete&#8221; link to go next to it before adding it to the DOM (the call to the <code>.html()<\/code> function in the referenced code).  You&#8217;ll want to make sure that you give the added HTML a unique <code>id<\/code> (wrapped in a <code>div<\/code> with a unique <code>id<\/code> for example), as well as to the added &#8220;Delete&#8221; link.<\/p>\n<p>Then, after it&#8217;s added to the DOM, you&#8217;d add an event handler to the &#8220;Delete&#8221; link:<\/p>\n<pre><code>$(\"#deleteLink123\").click(function() {\n<\/code><\/pre>\n<p>In this handler you&#8217;d reference the unique <code>id<\/code> of the container of what was added to the DOM and <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/api.jquery.com\/remove\/\">remove it<\/a>.  I&#8217;m sure there&#8217;s a more elegant way to handle this by using the <code>.live()<\/code> function <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/api.jquery.com\/live\/\">here<\/a> and some more clever <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/api.jquery.com\/category\/selectors\/\">selectors<\/a>.  It would be interesting to re-factor something like this while trying to not be too clever so as to hurt ongoing support.<\/p>\n<p>There are some additional considerations you&#8217;ll want to keep in mind as you develop this:<\/p>\n<ol>\n<li>The anchor tags are by default links, but it looks like you don&#8217;t want them to link to anything.  You can link them to &#8220;#&#8221; but that&#8217;s not really elegant.  You could remove the <code>href<\/code> attribute from them entirely, but then you&#8217;ll also need to style them to visually behave like links.  Not difficult, but not intuitive either.  Why not buttons?<\/li>\n<li>How the actual form behaves when handled by the server is up to you.  You can dynamically add elements to the DOM to your heart&#8217;s content, but that may not help you on the server-side.  That&#8217;s another question entirely, though.<\/li>\n<\/ol>\n<\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">3<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Dynamically add an input to an HTML form [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] I&#8217;m struggling whether to vote to close this as a duplicate, even though it&#8217;s not exact. But what you&#8217;re looking for appears to be answered here. The main difference is that where they are binding to the change event for a select element: $(&#8220;#selectBox&#8221;).change(function() { you would instead bind to the click event of &#8230; <a title=\"[Solved] Dynamically add an input to an HTML form [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-dynamically-add-an-input-to-an-html-form-closed\/\" aria-label=\"More on [Solved] Dynamically add an input to an HTML form [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[464,346,1148,333,388],"class_list":["post-29269","post","type-post","status-publish","format-standard","hentry","category-solved","tag-css","tag-html","tag-input","tag-javascript","tag-jquery"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Dynamically add an input to an HTML form [closed] - JassWeb<\/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:\/\/jassweb.com\/solved\/solved-dynamically-add-an-input-to-an-html-form-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Dynamically add an input to an HTML form [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] I&#8217;m struggling whether to vote to close this as a duplicate, even though it&#8217;s not exact. But what you&#8217;re looking for appears to be answered here. The main difference is that where they are binding to the change event for a select element: $(&quot;#selectBox&quot;).change(function() { you would instead bind to the click event of ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-dynamically-add-an-input-to-an-html-form-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-06T10:03:51+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-dynamically-add-an-input-to-an-html-form-closed\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-dynamically-add-an-input-to-an-html-form-closed\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Dynamically add an input to an HTML form [closed]\",\"datePublished\":\"2023-01-06T10:03:51+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-dynamically-add-an-input-to-an-html-form-closed\/\"},\"wordCount\":358,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"css\",\"html\",\"input\",\"javascript\",\"jquery\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-dynamically-add-an-input-to-an-html-form-closed\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-dynamically-add-an-input-to-an-html-form-closed\/\",\"name\":\"[Solved] Dynamically add an input to an HTML form [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2023-01-06T10:03:51+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-dynamically-add-an-input-to-an-html-form-closed\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-dynamically-add-an-input-to-an-html-form-closed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-dynamically-add-an-input-to-an-html-form-closed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Dynamically add an input to an HTML form [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Dynamically add an input to an HTML form [closed] - JassWeb","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:\/\/jassweb.com\/solved\/solved-dynamically-add-an-input-to-an-html-form-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Dynamically add an input to an HTML form [closed] - JassWeb","og_description":"[ad_1] I&#8217;m struggling whether to vote to close this as a duplicate, even though it&#8217;s not exact. But what you&#8217;re looking for appears to be answered here. The main difference is that where they are binding to the change event for a select element: $(\"#selectBox\").change(function() { you would instead bind to the click event of ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-dynamically-add-an-input-to-an-html-form-closed\/","og_site_name":"JassWeb","article_published_time":"2023-01-06T10:03:51+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-dynamically-add-an-input-to-an-html-form-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-dynamically-add-an-input-to-an-html-form-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Dynamically add an input to an HTML form [closed]","datePublished":"2023-01-06T10:03:51+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-dynamically-add-an-input-to-an-html-form-closed\/"},"wordCount":358,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["css","html","input","javascript","jquery"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-dynamically-add-an-input-to-an-html-form-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-dynamically-add-an-input-to-an-html-form-closed\/","name":"[Solved] Dynamically add an input to an HTML form [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2023-01-06T10:03:51+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-dynamically-add-an-input-to-an-html-form-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-dynamically-add-an-input-to-an-html-form-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-dynamically-add-an-input-to-an-html-form-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Dynamically add an input to an HTML form [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/29269","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=29269"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/29269\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=29269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=29269"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=29269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}