0

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't working.

jQuery('.advert')+ lu_ban_object.method +('<div>Hello World</div>')

So in my db I've got some values serialized; following is the data that is being localized to used as method chosen by the user

s:6:"method";s:5:".wrap";}

Essentially what it should do is;

jQuery('.advert').wrap('<div>Hello World</div>')

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;

jQuery('.advert')lu_ban_object.method('<div>Hello World</div>')
jQuery('.advert')jQuery(lu_ban_object.method)('<div>Hello World</div>')

Is just not working.... 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;

<select name="lu_ban_data[method]" id="lu_ban_data" />
        <option value=".append">Append</option>
        <option value=".prepend">Prepend</option>
        <option value=".wrap">Wrap</option>
        <option value=".parent().parent().wrap">Parent</option>

update: var dump

array (size=4)
  'title' => string 'My custom title' (length=15)
  'msg' => string 'My message' (length=10)
  'image' => string 'http://i.imgur.com/3tPjx4l.png' (length=30)
  'method' => string '.prepend' (length=8)

I tried the bracket solution but still nothing, jQuery('.advert')[lu_ban_object.method]("<div>Hello World</div>") I am getting the following error: Uncaught TypeError: Object [object Object] has no method '.prepend' So I added the plus symbols and the message went away but the div was still not being added to the page +[lu_ban_object.method]+

following is my localized data that is being printed in the header

<script type='text/javascript'>
/* <![CDATA[ */
var lu_ban_object = {"title":"My custom title","msg":"My message","image":"http:\/\/i.imgur.com\/3tPjx4l.png","method":".prepend"};
/* ]]> */
</script>

1 Answer 1

2

If lu_ban_object.method equals the string wrap, and you'd like to use that string to call jQuery's wrap() method, you'd use bracket notation :

jQuery(function($) {
    $('.advert')[lu_ban_object.method]('<div>Hello World</div>');
});
1
  • i didn't know about the bracket notation. thnx! +1
    – webdwall
    Commented Jul 15, 2013 at 6:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.