Skip to content

Instantly share code, notes, and snippets.

@eyupfidan
Last active May 4, 2023 12:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eyupfidan/fe9cc9821cbba336e05027ffb79b573b to your computer and use it in GitHub Desktop.
Save eyupfidan/fe9cc9821cbba336e05027ffb79b573b to your computer and use it in GitHub Desktop.
Live Search Jquery
// Refactored keyup event handler for the target link input
$(".target_link").on("keyup", function () {
const query = $(this).val();
if (query === "") {
$("#livesearch").html("No result found");
return;
}
$.ajax({
url: "../../action/project_article/live_search",
type: "GET",
data: { q: query },
success: function (results) {
results = JSON.parse(results);
let list = $("<ul>");
results.forEach(function (result) {
const listItem = $("<li>").addClass("hovered").text(result.domain_url).on("click", function () {
setValue(result.domain_url);
});
list.append(listItem);
});
$("#livesearch").html(list);
}
});
});
});
function setValue(value) {
const targetLink = $(".target_link");
const livesearch = $("#livesearch");
targetLink.val(value);
livesearch.empty();
const price = $(".price");
const contactMail = $(".contact-mail");
const contactedMail = $(".contacted-mail");
// Refactored AJAX request
$.ajax({
url: "../../action/project_article/get_live_search_data",
type: "POST",
data: { domainUrl: value },
success: function (data) {
const parsedData = JSON.parse(data)[0];
price.val(parsedData.price);
contactMail.val(parsedData.web_contact_mail);
contactedMail.val(parsedData.contacted_mail);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment