2

I am using an autocomplete Javascript function, and I need to extract the last 5 characters from 'value' and then compose URL for onSelect.

The function I am using is:

<script type="text/javascript">
    var options, a;
    jQuery(function(){
      var onAutocompleteSelect = function(value, data) {
          window.open('ITEM.PRO?id='+ value);
        }

      options = { 
      serviceUrl:'JQUERY-SEARCH.pro',
      onSelect: onAutocompleteSelect,
       };
      a = $('#query').autocomplete(options);

    });
    </script>

When I click on search result it loads "ITEM.PRO?id=Article Brand Model Year Description 35612", but I need load "ITEM.PRO?id=35612"

Please could you help me? I am a totally newbie with JS. Thank you all in advance!

1
  • Please post a sample output from JQUERY-SEARCH.pro. Is it sending a separate data array, besides for the suggestions array? Commented Aug 15, 2011 at 4:12

5 Answers 5

2

Instead of window.open('ITEM.PRO?id='+ value); could you do this?

window.open('ITEM.PRO?id='+ value.split(' ').pop());
Sign up to request clarification or add additional context in comments.

Comments

1

There are a few different ways to acheive this.

This simplest is to add

value = value.slice(-5);

right before

window.open('ITEM.PRO?id='+ value);

This sets value to its last 5 characters. Read here about the String.slice function.

If you want set the value to the last 'word', so to speak, delimited by spaces, you could do this instead:

value = value.split(" ").pop();

Another method would be to take the last continuous string of digits in the value. For that, you could use this:

value = value.match(/\d+/).pop();

Which method you use, of course, depends on what would work most reliably with the input you have.

1 Comment

@bozodz I thought that too, but he only asked for the last five digits. Maybe he knows that the id will always be five digits.
0

Try this

var onAutocompleteSelect = function(value, data) {
          window.open('ITEM.PRO?id='+ value.substring(value.length-5));
        }

1 Comment

it works perfect! Thank you very much. I need only the last 5 characters of the string. Thank you all :)
0

This is a terrible solution, but will work in the case you listed. I will edit if you post more details:

<script type="text/javascript">
    var options, a;
    jQuery(function(){
      var onAutocompleteSelect = function(value, data) {
          window.open('ITEM.PRO?id='+ value.match(/\d+/)[0]);
        }

      options = { 
      serviceUrl:'JQUERY-SEARCH.pro',
      onSelect: onAutocompleteSelect,
       };
      a = $('#query').autocomplete(options);

    });
</script>

value.match(/\d+/)[0] will match any digits in your string as any array, so we take the first item in that array.

Comments

0

When your IDs exceed 5 digits, your code will break (as has been stated in comments). You can also use the .split approach mentioned by @bordoz, the disadvantage being that spaces in any of the other words would break this solution. Or you could use:

var url = 'ITEM.PRO?id='+ value.replace(/[^\d\.]/g,'');

Which would fail only if any of the other word contain numbers. Which one best fits your situation?

2 Comments

Splits in other words wouldn't break bozdoz's solution, because he is taking the last token, delimited by spaces. That means it doesn't matter how many items separated by spaces there are, as long as the id is the last one.
@Peter - true :) I had somehow failed to see that.

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.