0

I need some help on an autocomplete, when I do a search everything works fine if the item is in the list, I get the ID and return it in the dedicated input.

But is there a way to return a code 9998 if the search fails?

The jquery part :

$input_idenseigne=$('div.id_enseigne span.cell-sub-wrapper span.cell-input input');
$input_enseigne=   $('div.enseigne span.cell-sub-wrapper span.cell-input input');     

      
    
       var getItems = function(term, callback) {
            loading = true;
            $.ajax( {
                type: 'GET',
                url: "XXX",
                dataType: "json",
                data: {
                term: term
                },
                success: function( data ) {
                    loading = true;
                    callback($.map(data, function ( item ) {
                        return {
                            value: item[0],
                            label: item[1]
                        }
                    }));
                    
                   
                },
          error: function ( data ) {
            alert('error');
          }
                
                    
            });
        };
    
        var fillFields = function(item) {
        };
    
    


      $('input.ui-autocomplete-input').each(function() {
                  
    $(this).blur(function(){
     var keyEvent = $.Event("keydown");
     keyEvent.keyCode = $.ui.keyCode.ENTER;
     $(this).trigger(keyEvent);
     
            
     if ($(this).val().length >= 3) {
       getItems($(this).val(), function(items) {
        if (items.length) {    }
               
       });
     }
    }).autocomplete({
 
    source: function(request, response) {
         getItems(request.term, response);
    },
    minLength: 3,
    search: function( event, ui ) { },
    select: function( event, ui)     {
        event.preventDefault();
        $ ('div.id_enseigne span.cell-sub-wrapper span.cell-input input').val(ui.item.value);
        $ ('input.ui-autocomplete-input').val(ui.item.label);
        
            }
  });
  });

And the html part :

<div class="answers answers-list">

  <div class="element even  groupingCols OneColumnEl hasError   id_enseigne ">
<span class="cell-sub-wrapper cell-legend-above">
<span class="cell-text cell-sub-column"></span>
<span class="cell-input cell-sub-column">
  <input type="text" name="ans3414" id="ans3414" value="" size="25">
</span>
</span>
</div>
<div class="element even  groupingCols OneColumnEl hasError enseigne ">
<span class="cell-sub-wrapper cell-legend-above">
<span class="cell-text cell-sub-column"></span>
<span class="cell-input cell-sub-column">
  <input type="text" name="ans3415" id="ans3415" value="" size="25" class="ui-autocomplete-input" autocomplete="off"/>
</span>
</span>
</div>

  </div>

Thanks you

I tried different methods with the following code for example, but it does not work:

,
          error: function ( data ) {
            alert('error');
          }
4
  • Just to clarify, are you trying to alert the error message returned from the server? Is the error method being called? Commented May 24, 2023 at 16:16
  • I tried with the alert method but nothing returned, ideally I would have liked to do something like this (I had placed the error method after the success in the code): error: function ( data ) { $ ('div.id_enseigne span.cell-sub-wrapper span.cell-input input').val('9998'); } Commented May 24, 2023 at 16:37
  • I am guessing that the server is not returning error. It is probably returning an empty array or object. Inside the success method just check and see if data is empty. If it is, alert whatever error message you like. Commented May 24, 2023 at 19:10
  • I tried what you said and it works, thanks! Commented May 25, 2023 at 8:37

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.