I’m using James Smith’s excellent tokenized input plugin for a search box of sorts. (PHP backend.) I’m trying to prepopulate the input but only some of the time – for example, if the user has passed my URL a parameter.
Normal initialization:
$("#tokenized").tokenInput("http://www.my.url/index.php", {
theme: "facebook",
});
As desired, this starts me with a blank box and everything’s good.
Using the nifty prePopulate paramater:
$("#tokenized").tokenInput("http://www.my.url/index.php", {
theme: "facebook",
prePopulate: [<?php echo json_encode($initialValue); ?>]
});
$initialValue is an array with ‘id’ and ‘name’. This also works great when it’s set to something. But that’s my problem – only some of the time will it be set. I’ve tried sending it a zero-element array as well as one with empty values. Setting the array to null produces the expected uncaught exception. The box refuses to not prepopulate if it’s one of these – creating an entry for ‘undefined’ or ‘{space}’. I want it to be empty, as the first example above.
How can I tell my page to only prepopulate if my initial value is set? Can I do some sort of if() condition inline to the initialization?
Also, jQuery beginner here – as above, still using ‘php echo’ to get my value across. Is there a better way?
Thanks!
echois perfectly fine. Try this:It only adds the
prePopulateparameter ifinitialValueis set.