Last active
May 4, 2023 12:53
-
-
Save eyupfidan/fe9cc9821cbba336e05027ffb79b573b to your computer and use it in GitHub Desktop.
Live Search Jquery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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