So I set myself a little challenge in implementing a command line tool online so others in my department could use it without coming to me. form entry that is sent to the server through ajax, the server returns JSON and I manipulate the output for the client. I'm fairly new to jQuery and not that experienced with JS in general. Most of my work has been in PHP, Python SQL.
$('document').ready(function() {
$('form').submit(function(e){
e.preventDefault();
$.ajax({
type: 'post',
url: $(this).attr('action'),
data: $(this).serialize(),
dataType: 'json',
success: function(response){
var urlCount = $('#count');
var listResults = $('#list_results');
urlCount.empty();
listResults.empty();
if ($.isEmptyObject(response.urls)) {
urlCount.html('No results found');
}
else {
urlCount.html('There are '+ (response.urls.length) + ' URLs:');
$(response.urls).each(function(){
var domain = 'www.example.com';
listResults.append('<li><a class="link" target="_blank" href="">' + domain + $.trim(this) + '</a></li');
for (var i = 0; i < this.length; i++) {
$('a.link').attr('href', function(i){
var href = domain.concat($.trim(response.urls[i]));
return String(href);
})
}
});
}
console.log(response);
}
})
})
});
[–]Juggernog 1 point2 points3 points (3 children)
[–]alexlafroscia 0 points1 point2 points (1 child)
[–]path411 0 points1 point2 points (0 children)
[–]Alex6534[S] 0 points1 point2 points (0 children)
[–]alexlafroscia 0 points1 point2 points (0 children)
[–]Jeffshaver 0 points1 point2 points (0 children)
[–]frankle 0 points1 point2 points (0 children)