{"id":533,"date":"2022-08-30T15:02:56","date_gmt":"2022-08-30T15:02:56","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/how-can-i-reuse-jquery-dynamic-tags-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:02:56","modified_gmt":"2022-08-30T15:02:56","slug":"how-can-i-reuse-jquery-dynamic-tags-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/how-can-i-reuse-jquery-dynamic-tags-collection-of-common-programming-errors\/","title":{"rendered":"How can I reuse JQuery dynamic tags?-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m using a function to add images to a site. These go into a DIV, using .html() After that&#8217;s done I need those same images to show an effect on hover, but nothing happens when I hover them. If I type the same code I use in the .html() function, same class, etc, hover works.<\/p>\n<p>After lots of attempts, I narrowed my problem to this sample. Turns out the problem is how the container DIV exposes the code added through .html() &#8230; and my hover functions can&#8217;t find the dynamically added tags.<\/p>\n<p>Here&#8217;s my sample code:<\/p>\n<pre><code>\n\nProblems\n\n\n\n<br id=\"TXToldone\" \/>This line was already here<br \/><br \/>\n\n\n\nfunction createnewone(){\n   $txthtml=\"<br id=\"\\&quot;TXTnewone\\&quot;\" \/>And this one got added dynamically<br \/><br \/>\";\n   $(\"#DIVnewone\").html($txthtml);\n};\n$(document).ready(function() {\n   createnewone();\n}); \n$(\"*\").on(\"click\", function(){\n   alert( $(this).attr(\"id\") );\n});\n\n\n\n\n<\/code><\/pre>\n<p><strong>Notes:<\/strong> &#8211; This is just a raw example of my real problem. This is not what I&#8217;m actually trying to accomplish, but solving this one would make me solve the real one \ud83d\ude09 &#8211; The first two functions are from the actual code &#8230; after document is ready I load the pics and recursively add the html required to each DIV &#8230; &#8211; Third function is just so you guys can see my problem &#8230; It should be showing an alert with the ID of the element you click on (and it does for the 1st paragraph) &#8230; but check what happens when you click the second one &#8230; you don&#8217;t see the ID for that paragraph but the ID for the container DIV instead! &#8211; (yes, it also gives alerts for the other parents, up to the document, but hey, it&#8217;s just an example)<\/p>\n<p>&#8211; If I manually add the paragraph to that same DIV, the &#8220;static&#8221; paragraph shows its ID when clicked on, but the &#8220;dynamic&#8221; one still shows the DIV ID.<\/p>\n<p>Please, advise?<br \/>\n(also, I need the script to stay at the bottom of my code, so it can &#8220;see&#8221; all the pics)<\/p>\n<p>EDIT so my point is clearer: I need a way to add content to a DIV so other functions see the new code.<\/p>\n<p>How would you guys replace this line so the sample code works as intended?<\/p>\n<pre><code>$(\"#DIVnewone\").html($txthtml);\n<\/code><\/pre>\n<ol>\n<li>\n<p>Have you tried appending the new element to the div rather than setting it via html(), though I believe .html() should work as well.<\/p>\n<pre><code>function createnewone(){\n   $txthtml=\"<br id=\"\\&quot;TXTnewone\\&quot;\" \/>And this one got added dynamically<br \/><br \/>\";\n   $(\"#DIVnewone\").append($txthtml);\n};\n<\/code><\/pre>\n<p>then you can use the below ON function (1.7+) to do what you need it to do when you have the mouse over the selector (&#8216;#TXTnewone&#8217;). Note that the below will need to go into the &#8220;<code>$(document).ready(function() { ...here... }<\/code>&#8221; function, this is to ensure the &#8220;body&#8221; tag has been loaded when assigning the on event to it.<\/p>\n<pre><code>$(document).ready(function() {\n    createnewone();\n    $(\"body\").on(\n        {\n              mouseenter: function () { $(this).css(\"color\",\"red\"); }\n            , mouseleave: function () { $(this).css(\"color\",\"black\"); }\n        }\n        , \"#TXTnewone\"\n    );\n});\n<\/code><\/pre>\n<p>Here is a jsFiddle of the above working.<\/p>\n<\/li>\n<li>\n<p>You&#8217;re probably looking for the <code>live()<\/code> function.<\/p>\n<p>or <code>on()<\/code>, after jQuery version 1.7<\/p>\n<blockquote>\n<p><strong>Description<\/strong><br \/>\nAttach an event handler for all elements which match the current selector, <em>now and in the future.<\/em><\/p>\n<\/blockquote>\n<\/li>\n<li>\n<p>Place your hover functions inside the event call that adds the dynamic content. Your hover functions are being evaluated before the content is added and aren&#8217;t re-evaluated after, causing them to be <code>undefined<\/code>.<\/p>\n<p><strong>EDIT<\/strong><br \/>\nRobert Harvey&#8217;s answer is much better than this, but the idea is the same.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 20:19:38. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m using a function to add images to a site. These go into a DIV, using .html() After that&#8217;s done I need those same images to show an effect on hover, but nothing happens when I hover them. If I type the same code I use in the .html() function, same class, etc, hover works. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-533","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/533","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=533"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/533\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}