{"id":2047,"date":"2013-01-20T10:09:55","date_gmt":"2013-01-20T10:09:55","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=2047"},"modified":"2013-01-20T10:09:55","modified_gmt":"2013-01-20T10:09:55","slug":"jquery-replace-textbox-replace-textbox-with-another-element-using-jquery-examples","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/jquery-replace-textbox-replace-textbox-with-another-element-using-jquery-examples\/","title":{"rendered":"jQuery Replace Textbox With Another Element Examples"},"content":{"rendered":"<p>When working with different form elements, there might have been a time when you needed to replace a certain element in the form with another. In this article, I am going to share with you an easy way to replace a form element with another &amp; specifically, use <em><strong>jQuery to\u00a0<\/strong><strong><em>replace textbox with another element.<\/em><\/strong><\/em>\u00a0Examples follow.<!--more--><\/p>\n<h2>How to use jQuery to Replace Textbox With Another Element<\/h2>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=2047\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<p>Let&#8217;s consider that we have a textbox with ID &#8220;states&#8217;. We would like to <strong>replace this textbox with a textarea<\/strong> with ID &#8220;message&#8221;. Let&#8217;s see how this textbox replacement can be done using jQuery.<\/p>\n<h3>Example 1: Replace Textbox with Textarea using jQuery<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Example 1\">&lt;form name=\"form1\" id=\"form1\" method=\"post\" action=\"#\"&gt;\n\n&lt;div id=\"states_container\"&gt;\n&lt;label for=\"states\"&gt;States&lt;\/label&gt;\n\n&lt;input type=\"text\" name=\"states\" id=\"states\"&gt;\n&lt;\/div&gt;\n\n&lt;br&gt;\n\n&lt;input type=\"submit\" name=\"sbt_replace_textbox_with_textarea\" id=\"sbt_replace_textbox_with_textarea\" value=\"Replace Textbox With Textarea\" \/&gt;\n\n&lt;input type=\"button\" name=\"sbt_reset\" class=\"reset\" value=\"Reset\"&gt;\n\n&lt;\/form&gt;\n\n&lt;script type=\"text\/javascript\" language=\"javascript\" &gt;\n\/\/ &lt;![CDATA[\t\n$(document).ready(function(){\n\n$('#sbt_replace_textbox_with_textarea').click(function(e) {\n\n\t$('#states').replaceWith( $('&lt;textarea id=\"message\"&gt;') );\n\n\te.preventDefault();\n\n});\n\n});\t\n\/\/ ]]&gt;\t\t\t\n&lt;\/script&gt;<\/pre>\n<p>When you run the above code &amp; hit the Replace Textbox With Textarea button, you will see that the textbox will be instantly replaced with the textarea. Simply click on &#8220;Reset&#8221; button to reset the form to it&#8217;s original state.<\/p>\n<h3>Example 2: Replace Textbox with Select Dropdown using jQuery<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Example 2\">&lt;form name=\"form1\" id=\"form1\" method=\"post\" action=\"#\"&gt;\n\n&lt;div id=\"states_container\"&gt;\n&lt;label for=\"states\"&gt;States&lt;\/label&gt;\n\n&lt;input type=\"text\" name=\"states\" id=\"states\"&gt;\n&lt;\/div&gt;\n\n&lt;input type=\"submit\" name=\"sbt_replace_textbox_with_select\" id=\"sbt_replace_textbox_with_select\" value=\"Replace Textbox With Select\" \/&gt;\n\n&lt;input type=\"button\" name=\"sbt_reset\" class=\"reset\" value=\"Reset\"&gt;\n\n&lt;\/form&gt;\n\n&lt;script type=\"text\/javascript\" language=\"javascript\" &gt;\n\/\/ &lt;![CDATA[\t\n$(document).ready(function(){\n\n$('#sbt_replace_textbox_with_select').click(function(e) {\n\n\t$('#states').replaceWith( $('&lt;select id=\"cities\"&gt;&lt;option value=\"TX\"&gt;TX&lt;\/option&gt;&lt;\/select&gt;') );\n\n\te.preventDefault();\n\n});\n\n$('.reset').click(function(e) {\n\n\tlocation.reload();\n\n});\n\n});\t\n\/\/ ]]&gt;\t\t\t\n&lt;\/script&gt;<\/pre>\n<p>When you run the above code &amp; hit the Replace Textbox With Select button, you will see that the textbox will be instantly replaced with the Select Dropdown. Simply click on &#8220;Reset&#8221; button to reset the form to it&#8217;s original state.<\/p>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=2047\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<p><strong>That&#8217;s it!<\/strong><\/p>\n<p>Do you know of any other ways to use <em><strong>jQuery to r<\/strong><strong>eplace Textbox<\/strong><\/em>? Feel free to suggest by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When working with different form elements, there might have been a time when you needed to replace a certain element in the form with another. In this article, I am going to share with you an easy way to replace a form element with another &amp; specifically, use jQuery to\u00a0replace textbox with another element.\u00a0Examples follow.<\/p>\n","protected":false},"author":1,"featured_media":395,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[20,13],"tags":[41],"class_list":["post-2047","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jquery-tips-and-tricks","category-tips-and-tricks","tag-jquery-tips"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paBnQX-x1","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/2047","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/comments?post=2047"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/2047\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/media?parent=2047"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=2047"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=2047"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}