The issue is that the imageSelect function is not being called when the click event is triggered on the img elements. This could be due to the fact that the img elements are being dynamically added to the DOM after the page has loaded. To fix this, you can use event delegation by attaching the click event listener to a parent element that exists on the page when it loads, and then checking if the target of the event is an img element with the upload_image class.
Here's an example of how you can modify the code to use event delegation:
function imageSelect(evt) {
alert('hello');
}
CKEDITOR.on('dialogDefinition', function (ev) {
var dialogName = ev.data.name;
var dialogDefinition = ev.data.definition;
if (dialogName == 'image') {
dialogDefinition.addContents({
id: 'uploadTab',
label: 'Upload',
elements: [{
type: 'text',
id: 'uploadField',
label: 'Search images',
onInput: function () {
var textBox = this.getInputElement();
var value = textBox.getValue();
if (value !== "") {
var URL = "https://pixabay.com/api/?key=" + API_KEY + "&q=" + encodeURIComponent(value);
var currentPage = 1;
var perPage = 100;
var totalPages = 0;
var totalHits = 0;
function showImages(data) {
if (parseInt(data.totalHits) > 0) {
totalHits = parseInt(data.totalHits);
totalPages = Math.ceil(totalHits / perPage);
var images = "";
$.each(data.hits, function (i, hit) {
images += "<img class='upload_image' src='" + hit.webformatURL + "' alt='" + hit.tags + "' width='50' height='60'>";
});
document.getElementById("upload_image_p").innerHTML = "";
document.getElementById("upload_image_p").innerHTML += "<div id='container'>" + images + "</div>";
// show pagination buttons
var pagination = "<div id='pagination'>";
pagination += "<button id='prevBtn'>Prev</button>";
pagination += "<button id='nextBtn1'>Next</button>";
pagination += "</div>";
document.getElementById("container").innerHTML += pagination;
// add event listeners to pagination buttons
var prevBtn = document.getElementById('prevBtn');
var nextBtn1 = document.getElementById('nextBtn1');
prevBtn.addEventListener('click', function () {
currentPage--;
getImages(currentPage);
nextBtn1.disabled = false;
});
if (currentPage === 1) {
prevBtn.disabled = true;
}
nextBtn1.addEventListener('click', function () {
currentPage++;
getImages(currentPage);
prevBtn.disabled = false;
});
if (currentPage === totalPages) {
nextBtn1.disabled = true;
}
} else {
document.getElementById("container").innerHTML = "No hits";
}
}
function getImages(page) {
var pageURL = URL + "&page=" + page + "&per_page=" + perPage;
$.getJSON(pageURL, function (data) {
showImages(data);
});
}
// show loading icon
document.getElementById("upload_image_p").innerHTML = '<img src="./assets/img/loading_icon.gif">';
// fetch first page of images
getImages(currentPage);
} else {
document.getElementById("upload_image_p").innerHTML = "";
document.getElementById("upload_image_p").innerHTML += '<img src="./assets/img/loading_icon.gif">';
}
}
},
{
type: 'html',
id: 'upload_html',
html: '<div id="upload_image_p"><img src="./assets/img/loading_icon.gif"></div>',
}
]
});
// attach click event listener to parent element
document.getElementById("container").addEventListener("click", function (event) {
// check if target element is an img with the upload_image class
if (event.target && event.target.matches("img.upload_image")) {
imageSelect(event);
}
});
}
});