html - JQuery UI Autocomplete: how to strip tags from label before inserting into the input field? -
i'm using jquery ui autocomplete, have set of items returned via ajax terms highlighted search engine (so label contains html). problem every time choose item value html tags inserted text box of course not nice. wonder if there anyway can strip tags label before inserting text box
var cache = {}, lastxhr; $('#location_name').autocomplete({ source: function( request, response ) { var q = request.term; if ( q in cache ) { response( cache[ q ] ); return; } lastxhr = $.getjson( "location_search/", {q: q}, function( data, status, xhr ) { $.each(data, function(i, v){ data[i].label = v.name + (v.address !== null ? ', ' + v.address : ''); }); cache[ q ] = data; if ( xhr === lastxhr ) { response( cache [q] ); } }); }, select: function( event, ui ) { $('#location_id').val(ui.item.id); $('#selected_location').html(ui.item.label); } }).data( "autocomplete" )._renderitem = function( ul, item ) { return $( "<li></li>" ) .data( "item.autocomplete", item ) .append( "<a>"+ item.label + "</a>" ) // + + "<br>" + item.desc + "</a>" .appendto( ul ); };
add function:
function striphtml(oldstring) { return oldstring.replace(/<[^>]*>/g, ""); }
and add code:
$('#location_name').change(function(){ $(this).val(striphtml($(this).val()); });
Comments
Post a Comment