{"id":291,"date":"2014-05-19T14:05:31","date_gmt":"2014-05-19T14:05:31","guid":{"rendered":"http:\/\/www.se80.co.uk\/training-education\/?p=291"},"modified":"2014-07-24T08:38:33","modified_gmt":"2014-07-24T08:38:33","slug":"remove-and-add-html-element-using-javascript","status":"publish","type":"post","link":"https:\/\/www.se80.co.uk\/training-education\/remove-and-add-html-element-using-javascript\/","title":{"rendered":"Remove and Add HTML element using Javascript"},"content":{"rendered":"<p>When creating your SAP BSP using HTML you may want to add and remove\u00a0elements dynamically using JavaScript and\u00a0sometimes simply setting\u00a0the\u00a0element to visible or not isn&#8217;t the solution you are after. Sometimes\u00a0I have found that\u00a0even though\u00a0an element has been\u00a0set to hidden its footprint is still left behind and interferes with the alignment of other fields. Yes you could maybe change the structure\/style of your HTML code so this does not happen but it is possible to use JavaScript to add and remove elements as\u00a0and when\u00a0your require.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Original HTML code<br \/>\n<\/strong>&lt;li\u00a0id=&#8221;backButton&#8221;&gt;<br \/>\n&lt;a\u00a0id=&#8221;mybackLink&#8221; href=&#8221;?OnInputProcessing(goback)&#8221;\u00a0class=&#8221;myButton&#8221;\u00a0\u00a0&gt;Back\u00a0to previous&lt;\/a&gt;<br \/>\n&lt;\/li&gt;<\/p>\n<p><strong><br \/>\nRemove HTML elements<br \/>\n<\/strong>Removing an element is easy simply use the following JavaScript code:<\/p>\n<p>var\u00a0delbutton\u00a0=\u00a0document.getElementById(&#8216;myBackLink&#8217;);<br \/>\ndelbutton.parentNode.removeChild(delbutton);<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Add HTML elements<br \/>\n<\/strong>This is a little more tricky as you need to manually re-build the element again. I say tricky, it just needs a few more lines of code which you can copy and paste from below. Please note that there are other, maybe better ways to do this. This is just the method used after much Google searching. It will at least give you a start\u00a0especially if your background is SAP and you are just getting into the world of JavaScript\/HTML etc.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Re-adding Link (a href) back to your HTML page using \u00a0javascript<\/strong><br \/>\nvar\u00a0addLink\u00a0=\u00a0document.createElement(&#8216;a&#8217;);<br \/>\nvar\u00a0linkText\u00a0=\u00a0document.createTextNode(&#8220;Back\u00a0to Index&#8221;);<br \/>\naddLink.appendChild(linkText);<br \/>\naddLink.title\u00a0=\u00a0&#8220;title&#8221;;<br \/>\naddLink.href\u00a0=\u00a0&#8220;index.htm&#8221;;<br \/>\naddLink.id\u00a0=\u00a0&#8220;mybackLink&#8221;;<br \/>\naddLink.setAttribute(&#8216;class&#8217;,\u00a0&#8216;myButton&#8217;);<br \/>\ndocument.getElementById(&#8216;backButton&#8217;).appendChild(addLink);<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Also here is the code required to add an input button instead of a link(a href)<\/strong><br \/>\n<span style=\"color: #444000;\">var addbut = document.createElement(\u2018input\u2019);<\/span><br style=\"color: #444000;\" \/><span style=\"color: #444000;\">addbut.type\u00a0 = \u201csubmit\u201d;<\/span><br style=\"color: #444000;\" \/><span style=\"color: #444000;\">addbut.value = \u201cBack to previous\u201d;<\/span><br style=\"color: #444000;\" \/><span style=\"color: #444000;\">addbut.name = \u201cbackbut\u2033;<\/span><br style=\"color: #444000;\" \/><span style=\"color: #444000;\">addbut.id = \u201cmybackButton\u2033;<\/span><br style=\"color: #444000;\" \/><span style=\"color: #444000;\">addbut.setAttribute(\u2018class\u2019, \u2018test\u2019);<\/span><br style=\"color: #444000;\" \/><span style=\"color: #444000;\">document.getElementById(\u2018backbutton\u2032).appendChild(addbut);<\/span><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When creating your SAP BSP using HTML you may want to add and remove\u00a0elements dynamically using JavaScript and\u00a0sometimes simply setting\u00a0the\u00a0element to visible or not isn&#8217;t the solution you are after. Sometimes\u00a0I have found that\u00a0even though\u00a0an element has been\u00a0set to hidden its footprint is still left behind and interferes with the alignment of other fields. Yes [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[22,16],"tags":[23,24],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Remove and Add HTML element using Javascript - SAP Help, Training and Education<\/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:\/\/www.se80.co.uk\/training-education\/remove-and-add-html-element-using-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Remove and Add HTML element using Javascript - SAP Help, Training and Education\" \/>\n<meta property=\"og:description\" content=\"When creating your SAP BSP using HTML you may want to add and remove\u00a0elements dynamically using JavaScript and\u00a0sometimes simply setting\u00a0the\u00a0element to visible or not isn&#8217;t the solution you are after. Sometimes\u00a0I have found that\u00a0even though\u00a0an element has been\u00a0set to hidden its footprint is still left behind and interferes with the alignment of other fields. Yes [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.se80.co.uk\/training-education\/remove-and-add-html-element-using-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"SAP Help, Training and Education\" \/>\n<meta property=\"article:published_time\" content=\"2014-05-19T14:05:31+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2014-07-24T08:38:33+00:00\" \/>\n<meta name=\"author\" content=\"se80\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"se80\" \/>\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\":\"WebPage\",\"@id\":\"https:\/\/www.se80.co.uk\/training-education\/remove-and-add-html-element-using-javascript\/\",\"url\":\"https:\/\/www.se80.co.uk\/training-education\/remove-and-add-html-element-using-javascript\/\",\"name\":\"Remove and Add HTML element using Javascript - SAP Help, Training and Education\",\"isPartOf\":{\"@id\":\"https:\/\/www.se80.co.uk\/training-education\/#website\"},\"datePublished\":\"2014-05-19T14:05:31+00:00\",\"dateModified\":\"2014-07-24T08:38:33+00:00\",\"author\":{\"@id\":\"https:\/\/www.se80.co.uk\/training-education\/#\/schema\/person\/ddc6a049c1b0790f62d70278be75e511\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.se80.co.uk\/training-education\/remove-and-add-html-element-using-javascript\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.se80.co.uk\/training-education\/remove-and-add-html-element-using-javascript\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.se80.co.uk\/training-education\/remove-and-add-html-element-using-javascript\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.se80.co.uk\/training-education\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Remove and Add HTML element using Javascript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.se80.co.uk\/training-education\/#website\",\"url\":\"https:\/\/www.se80.co.uk\/training-education\/\",\"name\":\"SAP Help, Training and Education\",\"description\":\"Learn SAP\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.se80.co.uk\/training-education\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.se80.co.uk\/training-education\/#\/schema\/person\/ddc6a049c1b0790f62d70278be75e511\",\"name\":\"se80\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.se80.co.uk\/training-education\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/3fd4e388f05bf3c763e46cd1f77afeb2?s=96&d=retro&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/3fd4e388f05bf3c763e46cd1f77afeb2?s=96&d=retro&r=g\",\"caption\":\"se80\"},\"url\":\"https:\/\/www.se80.co.uk\/training-education\/author\/se80\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Remove and Add HTML element using Javascript - SAP Help, Training and Education","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:\/\/www.se80.co.uk\/training-education\/remove-and-add-html-element-using-javascript\/","og_locale":"en_US","og_type":"article","og_title":"Remove and Add HTML element using Javascript - SAP Help, Training and Education","og_description":"When creating your SAP BSP using HTML you may want to add and remove\u00a0elements dynamically using JavaScript and\u00a0sometimes simply setting\u00a0the\u00a0element to visible or not isn&#8217;t the solution you are after. Sometimes\u00a0I have found that\u00a0even though\u00a0an element has been\u00a0set to hidden its footprint is still left behind and interferes with the alignment of other fields. Yes [&hellip;]","og_url":"https:\/\/www.se80.co.uk\/training-education\/remove-and-add-html-element-using-javascript\/","og_site_name":"SAP Help, Training and Education","article_published_time":"2014-05-19T14:05:31+00:00","article_modified_time":"2014-07-24T08:38:33+00:00","author":"se80","twitter_card":"summary_large_image","twitter_misc":{"Written by":"se80","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.se80.co.uk\/training-education\/remove-and-add-html-element-using-javascript\/","url":"https:\/\/www.se80.co.uk\/training-education\/remove-and-add-html-element-using-javascript\/","name":"Remove and Add HTML element using Javascript - SAP Help, Training and Education","isPartOf":{"@id":"https:\/\/www.se80.co.uk\/training-education\/#website"},"datePublished":"2014-05-19T14:05:31+00:00","dateModified":"2014-07-24T08:38:33+00:00","author":{"@id":"https:\/\/www.se80.co.uk\/training-education\/#\/schema\/person\/ddc6a049c1b0790f62d70278be75e511"},"breadcrumb":{"@id":"https:\/\/www.se80.co.uk\/training-education\/remove-and-add-html-element-using-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.se80.co.uk\/training-education\/remove-and-add-html-element-using-javascript\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.se80.co.uk\/training-education\/remove-and-add-html-element-using-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.se80.co.uk\/training-education\/"},{"@type":"ListItem","position":2,"name":"Remove and Add HTML element using Javascript"}]},{"@type":"WebSite","@id":"https:\/\/www.se80.co.uk\/training-education\/#website","url":"https:\/\/www.se80.co.uk\/training-education\/","name":"SAP Help, Training and Education","description":"Learn SAP","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.se80.co.uk\/training-education\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.se80.co.uk\/training-education\/#\/schema\/person\/ddc6a049c1b0790f62d70278be75e511","name":"se80","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.se80.co.uk\/training-education\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/3fd4e388f05bf3c763e46cd1f77afeb2?s=96&d=retro&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3fd4e388f05bf3c763e46cd1f77afeb2?s=96&d=retro&r=g","caption":"se80"},"url":"https:\/\/www.se80.co.uk\/training-education\/author\/se80\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.se80.co.uk\/training-education\/wp-json\/wp\/v2\/posts\/291"}],"collection":[{"href":"https:\/\/www.se80.co.uk\/training-education\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.se80.co.uk\/training-education\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.se80.co.uk\/training-education\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.se80.co.uk\/training-education\/wp-json\/wp\/v2\/comments?post=291"}],"version-history":[{"count":11,"href":"https:\/\/www.se80.co.uk\/training-education\/wp-json\/wp\/v2\/posts\/291\/revisions"}],"predecessor-version":[{"id":398,"href":"https:\/\/www.se80.co.uk\/training-education\/wp-json\/wp\/v2\/posts\/291\/revisions\/398"}],"wp:attachment":[{"href":"https:\/\/www.se80.co.uk\/training-education\/wp-json\/wp\/v2\/media?parent=291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.se80.co.uk\/training-education\/wp-json\/wp\/v2\/categories?post=291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.se80.co.uk\/training-education\/wp-json\/wp\/v2\/tags?post=291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}