0

I want to check what kind of pattern the user is typing and based on that i want to make a server call. Could someone help here?

Here is what i have :

$(document).ready(function() {
    $("#search_frwId").tokenInput("http://some rest service here", {
        theme: "facebook",
        queryParam : "param"
    });
});
n1k1ch
  • 2,594
  • 3
  • 31
  • 35
af_khan
  • 1,030
  • 4
  • 25
  • 49
  • This sort of activity would generally be done on the web server. Can you clarify - do you wish to change the `http://some rest service here` address depending on the pattern the user is typing? – Chris Feb 27 '14 at 10:20

1 Answers1

0

It's possible to pass a function as the first parameter of the setup (in place of a static URL.) If you can get user-input through jQuery, then you may be best to create a custom function to pattern match and generate the relevent URL string.

i.e.

$(document).ready(function() {
    $("#search_frwId").tokenInput(getMyRestServer, {
        theme: "facebook",
        queryParam : "param"
    });
});

function getMyRestServer(){
    var userinput = $("#search_frwId-input-facebook-theme").attr('value');
    // or if Jquery 2.x then use
    //  var userinput = $("#search_frwId-input-facebook-theme").prop('value');
    var url = "";

    //Do Magic Pattern Matching here

    return url;
}

You need to pass the function in there, not its evaluation

af_khan
  • 1,030
  • 4
  • 25
  • 49
Chris
  • 5,882
  • 2
  • 32
  • 57
  • Its invoked only once on document load but not after that. any idea ? – af_khan Feb 28 '14 at 07:10
  • @Yahiya - wait, thought I had it, but no. No, as far as I can see, it should call the function every time, whether the results are cached or not. What plugin version are you using/are you able to link to a demo, or set up a jsfiddle? – Chris Feb 28 '14 at 10:02
  • Hi Chris...thanks. Your code worked with a small change. I've changed getMyRestServer() to getMyRestServer. – af_khan Mar 01 '14 at 21:02
  • *Facepalm* Good spot - sorry. – Chris Mar 02 '14 at 00:43
  • somehow am not able to get the value of the search text box with var query = $(".token-input-input-token").text(); or var query = $("#seach_id").val(); any idea ? – af_khan Mar 02 '14 at 09:27
  • @Yahiya Try `$(".token-input-input-token").('input').val()`? I think that might be it - but don't have a set up to debug with at the moment. If that doesn't do it - have a look at the code with Firebug/Chrome Inspector or something, and try and work out the exact jQuery - the input is wrapped inside a list and all sorts of other things! – Chris Mar 02 '14 at 11:43
  • tried for half a day... no use..dint get any solution yet. – af_khan Mar 02 '14 at 18:59
  • Are you able to share a link to your page? – Chris Mar 02 '14 at 19:01
  • Thank you for the fiddle - you need to use `var userinput = $("#token-input-demo-input-facebook-theme").attr('value');` – Chris Mar 02 '14 at 19:20
  • (Or `$("#search_frwId-input-facebook-theme").attr('value');` if the id of you token input is the same as what you put in the original question.) – Chris Mar 02 '14 at 19:22
  • getting value as undefined...i tried in it this fiddle http://jsfiddle.net/HL8HF/3/ – af_khan Mar 02 '14 at 19:28
  • Thanks a lot Chris. attr('value') dint work for me but i tried prop('value') and it worked. May be because i am using Jquery 2.x. Anyways, thanks a lot for your help... Now i am gonna start pattern matching using that userinput and call the appropriate rest service. – af_khan Mar 02 '14 at 20:21