{"id":245,"date":"2010-12-27T15:11:45","date_gmt":"2010-12-27T15:11:45","guid":{"rendered":"http:\/\/www.osd.net\/blog\/?p=245"},"modified":"2014-10-03T12:33:43","modified_gmt":"2014-10-03T12:33:43","slug":"place-element-over-another-in-javascript","status":"publish","type":"post","link":"https:\/\/www.osd.net\/blog\/web-development\/javascript\/place-element-over-another-in-javascript\/","title":{"rendered":"Place element over another in javascript"},"content":{"rendered":"<p>If you want to place an html element over another one using javascript for some reason here you will find the solution. Why you might want to do this? Maybe you want to place an &#8220;online now&#8221; stamp on the corner of the user avatar or an &#8220;on sale&#8221; stamp on some of the products on your online store.<\/p>\n<p>This will be something like an watermark but without editing the image. This has two main characteristics: it can be dynamically updated (like the online status of an user or the promotions on your store) but it is not the solution for protecting your pictures.<\/p>\n<p>Since there are a lot of watermark solutions out there I will focus only on this other matter.<\/p>\n<p>So what&#8217;s the first thing we need? The absolute position of the element you want to apply the stamp on.<\/p>\n<h1>Getting the absolute position of an element using JavaScript<\/h1>\n<p>You can do this using this function:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction getXYpos(elem)\r\n{\r\n   if (!elem)\r\n   {\r\n      return {&quot;x&quot;:0,&quot;y&quot;:0};\r\n   }\r\n   var xy={&quot;x&quot;:elem.offsetLeft,&quot;y&quot;:elem.offsetTop}\r\n   var par=getXYpos(elem.offsetParent);\r\n   for (var key in par)\r\n   {\r\n      xy[key]+=par[key];\r\n   }\r\n   return xy;\r\n}\r\n<\/pre>\n<p>This function goes from the element you provide through all of its parents and finds out the absolute position by adding the relative positions.<\/p>\n<p>Now we need the final function: the one that places the stamp.<\/p>\n<h1>Add element over another using JavaScript<\/h1>\n<p>This is a pretty simple function but it also has comments for almost every line.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\t\tfunction setStamp()\r\n\t\t{\r\n\t\t\t\/\/ create a new element.. the one that we will place on top\r\n\t\t\tvar new_el = document.createElement('img');\r\n\t\t\t\/\/ set the source for the image element\r\n\t\t\tnew_el.setAttribute('src', 'online.png');\r\n\t\t\t\/\/ add an id to the element so we could find it later\r\n\t\t\tnew_el.setAttribute('id', 'photoStamp');\r\n\t\t\t\/\/ set absolute position to the new element\r\n\t\t\tnew_el.style.position = 'absolute';\r\n\t\t\t\/\/ this is the picture we need to set the stamp on\r\n\t\t\tvar main_pic = document.getElementById('userAvatar');\r\n\t\t\t\/\/ get the coordinates for this &quot;parent&quot;\r\n\t\t\tvar big_coordinates=getXYpos(main_pic);\r\n\t\t\tvar bp_x = big_coordinates['x'];\r\n\t\t\tvar bp_y = big_coordinates['y'];\r\n\t\t\t\/\/ set these values for the new created element\r\n\t\t\tnew_el.style.left =  bp_x + 'px';\r\n\t\t\tnew_el.style.top = bp_y + 'px';\r\n\t\t\t\/\/ get the container for the picture\r\n\t\t\tvar container = document.getElementById('userAvatarContainer');\r\n\t\t\t\/\/ add the new element as child for the container\r\n\t\t\tcontainer.appendChild(new_el);\r\n\t\t}\r\n<\/pre>\n<p>That&#8217;s all. But there are some improvements we can add.<\/p>\n<p>What happens if the window is resized? The new element (that has an absolute position) will no longer be on top of its parent. What do we need to do? Call this function again so the absolute coordinates will be the new ones. How? Like this:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nwindow.onresize=setStamp;\r\n<\/pre>\n<p>But the &#8220;old&#8221; stamp will still be there. So we need to update our function so that every time we call it it would check if the stamp exists and remove it. There is no direct way to remove an element using javascript but there is a way to remove the child of an element. So we will add something like this:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nelm.parentNode.removeChild(elm);\r\n<\/pre>\n<p>So now we are ok, this function can be called by another one (ajax based) that checks the user\/product status at a timed interval or any other way you want.<\/p>\n<p>What are the useful things this small script takes advantage of:<\/p>\n<ul>\n<li>getting the absolute position of an element<\/li>\n<li>removing an element<\/li>\n<li>creating a new element<\/li>\n<li>assigning absolute positions to an element.<\/li>\n<\/ul>\n<p>All these using js.<\/p>\n<p>As I got used after getting a nice feedback from one of the readers I will give you a download link for the full working example: <a href=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2010\/12\/absolutepos.zip\">this is it \ud83d\ude42<\/a><\/p>\n<p>If you have any ideas or questions please feel free to post them as a comment to this post using the form below.<\/p>\n<div id=\"share-and-beer-container\">\t<div id=\"buy_me_a_beer_div\" class=\"single beer\">\n\t \t\n\t\t<div class=\"buy-beer\" onclick=\"document.getElementById('buy_me_a_beer_form').submit();\">\n\t\t\t<form action=\"https:\/\/www.paypal.com\/cgi-bin\/webscr\" id=\"buy_me_a_beer_form\" method=\"post\">\n\t\t\t\t<input type=\"hidden\" name=\"cmd\" value=\"_xclick\" \/>\n\t\t\t\t<input type=\"hidden\" name=\"business\" value=\"info@directaccess.ro\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"item_name\" value=\"A Beer For Place element over another in javascript\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"item_number\" value=\"1\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"return\" value=\"https:\/\/www.osd.net\/blog\/web-development\/javascript\/place-element-over-another-in-javascript\/\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"amount\" value=\"5\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"undefined_quantity\" value=\"1\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"currency_code\" value=\"USD\" \/>  \n\t\t\t<\/form>\n\t\t\t<p class=\"buy-beer-text\">If you liked this post <br \/> you can <strong>buy me a beer<\/strong><\/p>\n\t\t<\/div>\n\t<\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>If you want to place an html element over another one using javascript for some reason here you will find the solution. Why you might want to do this? Maybe you want to place an &#8220;online now&#8221; stamp on the corner of the user avatar or an &#8220;on sale&#8221; stamp on some of the products &hellip;<\/p>\n<div class=\"cta1\"><a href=\"https:\/\/www.osd.net\/blog\/web-development\/javascript\/place-element-over-another-in-javascript\/\">Read more<\/a><\/div>\n<div class=\"like-excerpt\"><div\n        class=\"fb-like\"\n        data-href=\"https:\/\/www.osd.net\/blog\/web-development\/javascript\/place-element-over-another-in-javascript\/\"\n        data-layout=\"button_count\"\n        data-action=\"like\"\n        data-show-faces=\"false\"\n        data-share=\"false\">\n        <\/div><\/div>","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[18],"tags":[62,55,56],"_links":{"self":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/245"}],"collection":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/comments?post=245"}],"version-history":[{"count":11,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/245\/revisions"}],"predecessor-version":[{"id":1297,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/245\/revisions\/1297"}],"wp:attachment":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/media?parent=245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/categories?post=245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/tags?post=245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}