{"id":1489,"date":"2022-08-30T15:16:57","date_gmt":"2022-08-30T15:16:57","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/23\/localizing-methods-for-jquery-append-prepend-wrap-parent-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:16:57","modified_gmt":"2022-08-30T15:16:57","slug":"localizing-methods-for-jquery-append-prepend-wrap-parent-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/localizing-methods-for-jquery-append-prepend-wrap-parent-collection-of-common-programming-errors\/","title":{"rendered":"Localizing methods for jQuery append prepend wrap parent-Collection of common programming errors"},"content":{"rendered":"<p>So I am serializing some options in WordPress and then localizing them to be used in my jQuery script, however what I am trying to do next isn&#8217;t working.<\/p>\n<pre><code>jQuery('.advert')+ lu_ban_object.method +('Hello World')\n<\/code><\/pre>\n<p>So in my db I&#8217;ve got some values serialized; following is the data that is being localized to used as method chosen by the user<\/p>\n<blockquote>\n<p>s:6:&#8221;method&#8221;;s:5:&#8221;.wrap&#8221;;}<\/p>\n<\/blockquote>\n<p>Essentially what it should do is;<\/p>\n<pre><code>jQuery('.advert').wrap('Hello World')\n<\/code><\/pre>\n<p>The user can also append, prepend, wrap, etc but it is just not working, the methods are being localized and serialized so it should work, but it isnt. I have tried the following as well;<\/p>\n<pre><code>jQuery('.advert')lu_ban_object.method('Hello World')\njQuery('.advert')jQuery(lu_ban_object.method)('Hello World')\n<\/code><\/pre>\n<p>Is just not working&#8230;. anyone know how to solve this. or am I doing it the wrong way? I just have a list in the settings page and each has a value for a different method such as;<\/p>\n<pre><code>\n        Append\n        Prepend\n        Wrap\n        Parent\n<\/code><\/pre>\n<p><strong>update: var dump<\/strong><\/p>\n<pre><code>array (size=4)\n  'title' =&gt; string 'My custom title' (length=15)\n  'msg' =&gt; string 'My message' (length=10)\n  'image' =&gt; string 'http:\/\/i.imgur.com\/3tPjx4l.png' (length=30)\n  'method' =&gt; string '.prepend' (length=8)\n<\/code><\/pre>\n<p>I tried the bracket solution but still nothing, <code>jQuery('.advert')[lu_ban_object.method](\"Hello World\")<\/code> I am getting the following error: <code>Uncaught TypeError: Object [object Object] has no method '.prepend'<\/code> So I added the plus symbols and the message went away but the div was still not being added to the page <code>+[lu_ban_object.method]+<\/code><\/p>\n<p>following is my localized data that is being printed in the header<\/p>\n<pre><code>\n\/*  *\/\n\n<\/code><\/pre>\n<ol>\n<li>\n<p>If <code>lu_ban_object.method<\/code> equals the string <code>wrap<\/code>, and you&#8217;d like to use that string to call jQuery&#8217;s <code>wrap()<\/code> method, you&#8217;d use bracket notation :<\/p>\n<pre><code>jQuery(function($) {\n    $('.advert')[lu_ban_object.method]('Hello World');\n});\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-23 09:50:41. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>So I am serializing some options in WordPress and then localizing them to be used in my jQuery script, however what I am trying to do next isn&#8217;t working. jQuery(&#8216;.advert&#8217;)+ lu_ban_object.method +(&#8216;Hello World&#8217;) So in my db I&#8217;ve got some values serialized; following is the data that is being localized to used as method chosen [&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-1489","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1489","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=1489"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1489\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1489"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1489"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1489"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}