I have a Javascript code that sends an Ajax request and upon receiving the data, it creates an "a" tag with an onclick event that triggers another method along with a parameter. Below is the implementation:
function loadHistory() {
var detailsForGet = {
userId: sessionStorage.getItem("userName")
}
$.ajax({
type: "GET",
url: "https://localhost:44326/User/getTwitterSearchHistory",
data: detailsForGet,
success: function (data) {
jQuery.each(data, function (index, item) {
console.log(item)
$('<a href="#" onclick="historySearch(' + item + ')">' + item + '</a><br/>').appendTo('#forHistory');
});
}
});
}
function historySearch(text) {
console.log(text)
document.getElementById('tweetSearching').innerHTML = "Searching...";
$.getJSON("https://localhost:44326/api/Twitter/GetSearchResults?searchString=" + text)
.done(function (data) {
if (data.length == 0) {
document.getElementById('tweetSearching').innerHTML = "No matches Found!";
}
else {
document.getElementById('tweetSearching').innerHTML = "Found " + data.length + " matches!";
console.log(data)
document.getElementById('searchResults').innerHTML = "";
$('<h3 style="text-align:center; color: white">Search Results</h3> <hr/>').appendTo('#searchResults');
$.each(data, function (key, item) {
$('<ul style="list-style:none;"><li><span style="font-weight: bold; color: white">Post </span><span style="color: white">' + item.FullText + '</span><br/><span style="font-weight: bold; color: white">Uploader</span><span style="color: white"> ' + item.CreatedBy.Name + " @" + item.CreatedBy.ScreenName + '</span><br/><span style="font-weight: bold; color: white">Date</span><span style="color: white"> ' + formatDateTwitter(item.CreatedAt) + '</span><li/><hr/></ul>').appendTo('#searchResults');
});
}
});
}
When I attempt to click on the "a" tag, I encounter an error
Uncaught SyntaxError: missing ) after argument list
This issue only arises when calling a method with a parameter in the onclick function. Previously, I tried calling a function without a parameter, and it worked fine. However, passing a parameter results in this error.
Am I missing something important here?