I built an auto-complete function for my website that queries a database for results. I currently can get the results, make them into hyperlinks, and dynamically add them to the page. Once in a while I get results that are very long (40-50 characters) and I want to cut them down to 20 characters or less. Here is my current code:
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "autoCompleter2.asmx/GetDatabaseName",
data: '{"prefixText":"' + $('#tags').val() + '"}',
dataType: "json",
success: function(data) {
$(data.d).each(function(index, value) {
$("<a />").attr("href", "http://wms.pc.factset.com/sql_database.aspx?name=" + value).appendTo("#databaseHolder").text(value).after("<br />");
});
},
});
I have searched all over the internet but I cannot find how to take my “value” variable and cut it into a 20 character sub string. I have seen code like:
$(value).text().substring(0, 2);
but nothing works. Any help would be amazing.
This assumes that
valueis a sting.Here is a demo: http://jsfiddle.net/hEjxB/
If
valueis a DOM element then you can do this:Here is a demo: http://jsfiddle.net/hEjxB/1/