Question

My site has two ads on it, one of them uses a script which parses all links on the site and does preventDefault() on them... this makes it impossible for me to use preventDefault() on any links since it can only be done once... However in the past I've been able to work around this using return false. For some reason it doesn't work with the jQuery ui autocomplete function though (see code below).

If I turn off the ads the script works fine. Otherwise it just reloads the page because the return false; doesn't seem to work...

Script I cannot change:

$(document).ready(function() {  
    $('a').on('click', function(e) {
        e.preventDefault();
        // stuff
    });
)};

My script:

$search.autocomplete({
  source: function(request, response){
    $url = "suggestions/";
    $.get($url, {data:request.term}, function(data){     
        response($.map(data, function(item) {
        return {                
            label: item.movie_name,
            id: item.movie_id
        }
        }))
    }, "json");
  },
  minLength: 2,
  dataType: "json",
  cache: true,
  focus: function(event, ui) {
    return false;
  },
  select: function(event, ui) {
    window.location.href = ('/id/'+ ui.item.id +'/'+ ui.item.label +'/');
    return false;
  }
});
Was it helpful?

Solution

I found a solution that works:

  select: function(event, ui) {
     $('.ui-autocomplete a').attr('href', '/id/'+ ui.item.id +'/'+ ui.item.label +'/');     
  }

Explaination: The jquery-ui-autocomplete function creates a list of links ()-tags, normally you would use preventDefault() to stop the browser from going through to the href address, since in my case that is not possible I tried using return false instead but since that doesn't work either in this case I finally figured, why not change the link-tags href to the correct url instead and not halt the action, which works!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top