{"id":448,"date":"2022-08-30T15:01:31","date_gmt":"2022-08-30T15:01:31","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/ajax-does-not-post-to-div-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:01:31","modified_gmt":"2022-08-30T15:01:31","slug":"ajax-does-not-post-to-div-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/ajax-does-not-post-to-div-collection-of-common-programming-errors\/","title":{"rendered":"Ajax does not post to div-Collection of common programming errors"},"content":{"rendered":"<p>I receive no errors from either jshint or console for this code. Yet, my ajax content does not post to the intended div(s). The alerts, however, fire:<\/p>\n<pre><code>    var recentVar;\n    var highlightsVar;\n    var test3;\n    var test4;\n\nfunction switcher(divToShow, thisVar, otherVar, url, div){\n    $(\"#site-nav li a\").parents().removeClass(\"nav-active\");\n        $(div).addClass(\"nav-active\");\n        if (otherVar){ $(otherVar).detach();}\n\n        if(typeof thisVar === 'undefined') {\n            thisVar = $(divToShow + \" ul.top-level\").load(\"assets\/includes\/\" + url, function () {\n                alert(\"I'm new\");\n            });\n        } else {\n            $(thisVar).appendTo(\"#content-listing\");\n            alert(\"I'm old\");\n        }\n}\n\n    \/\/Recent\n    $(\"#site-nav .nav1\").on(\"click\", function (event) {\n    switcher(\"#recent\", \"recentVar\", \"highlightsVar\", \"recent.php\", \"#site-nav .nav1\");\n    event.preventDefault();\n    });\n\n    \/\/Highlights\n    $(\"#site-nav .nav2\").on(\"click\", function (event) {\n    switcher(\"#highlights\", \"highlightsVar\", \"recentVar\", \"all-web.php\", \"#site-nav .nav2\");\n    event.preventDefault();\n    });\n<\/code><\/pre>\n<ol>\n<li>\n<p>It has nothing to do with AJAX, but I think your issue is here.<\/p>\n<pre><code>$(thisVar)\n<\/code><\/pre>\n<p><code>thisVar<\/code> is simply text. If you check, you will see that <code>$(thisVar).length<\/code> is 0. This is because you are calling: <code>$(\"recentVar\")<\/code>, which will search for any elements of type , which clearly there are none. If your intent is to append that text, you can use <code>append<\/code> or create an element and set the text to <code>recentVar<\/code> and append that.<\/p>\n<p><strong>EDIT<\/strong>: Another issue:<\/p>\n<pre><code>$(divToShow + \"ul.top-level\")\n<\/code><\/pre>\n<p>You need a space here:<\/p>\n<pre><code>$(divToShow + \" ul.top-level\")\n<\/code><\/pre>\n<p>Otherwise you are trying to match <code>#recentul.top-level<\/code> which is nonsense.<\/p>\n<\/li>\n<li>\n<p>Upon execution of switcher, thisVar is always going to be a string. It&#8217;s never going to have typeof undefined. It also LOOKS like you want to pass in an object (due to the declared variables) but&#8230; as stated, you&#8217;re always passing in a string.<\/p>\n<p>[per James Montagne&#8217;s comment]<\/p>\n<\/li>\n<li>\n<p>Try this:<\/p>\n<pre><code>if(typeof thisVar === 'undefined') {\n        thisVar = $(divToShow + \" ul.top-level\").load(\"assets\/includes\/\" + url, function () {\n            alert(\"I'm new\");\n        });\n    } else {\n        $(thisVar).appendTo($(\"#content-listing\"));\n        alert(\"I'm old\");\n    }\n<\/code><\/pre>\n<\/li>\n<li>\n<p>AMAIK, jQuery&#8217;s <code>load()<\/code> method directly inserts the result into the calling object. Thus the result should get loaded into <code>$(divToShow + \" ul.top-level\")<\/code> element. Have you checked it?<\/p>\n<\/li>\n<li>\n<p>If the current document does not contain an element with an ID of <code>divToShow + \" ul.top-level\"<\/code> the <code>.load()<\/code> method is not executed.<\/p>\n<p>Try Firebug or such things to track request. Maybe something is wrong with your response.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 19:43:11. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I receive no errors from either jshint or console for this code. Yet, my ajax content does not post to the intended div(s). The alerts, however, fire: var recentVar; var highlightsVar; var test3; var test4; function switcher(divToShow, thisVar, otherVar, url, div){ $(&#8220;#site-nav li a&#8221;).parents().removeClass(&#8220;nav-active&#8221;); $(div).addClass(&#8220;nav-active&#8221;); if (otherVar){ $(otherVar).detach();} if(typeof thisVar === &#8216;undefined&#8217;) { thisVar = [&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-448","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/448","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=448"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/448\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=448"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}