HTML
<input type='text' id='searchable'value='hello world' />
<input type='hidden' id=x value='hel' />
how can i select textfrom #searchable starting from end of value of hidden input till end of #searchable ?
Fixed thanks all
$('input.complete').live('keyup.autocomplete', function(){
var hi=$(this).val().toUpperCase();
var was=this;//PROBlEM was here... was=$(this)
$(this).autocomplete({
source: function(request, response) {
$.ajax({ url: '<?=base_url()?>ajax/ac',
data: { 'term': this.term,'page': 'clinic','key': this.element.attr('data-complete')},
dataType: "json",
type: "POST",
success: function(data){
if(!data.length){
var result = [{label: 'No match found',value: response.term}];
response(result);
}else{
response(data);
$(was).val(data[0]['value']);
was.selectionStart=hi.length;
was.selectionEnd=data[0]['value'].length;
}
}
});
},
select: function(event, ui){},
minLength: 2,
});
});
Try this out
Live demo
If
xvalue always start matching at the first character of search’s value, thesearchvalue.indexOf(xtext)is redundant as it will always return0and can be safely removed.References:
selectionStart,selectionEndselectionStart,selectionEndHTMLInputElementPropertiesNote: As
selectionStartandselectionEndare part of HTML5, there’s no IE < 9 support. If you’re rolling out your own typeahead, I suggest feature testing these before applying the the autocomplete in the text input element if you plan to support old IE.