diff --git a/decidim-core/app/packs/src/decidim/append_redirect_url_to_modals.js b/decidim-core/app/packs/src/decidim/append_redirect_url_to_modals.js index 37d4aac0acf3..87e2bfcf77ae 100644 --- a/decidim-core/app/packs/src/decidim/append_redirect_url_to_modals.js +++ b/decidim-core/app/packs/src/decidim/append_redirect_url_to_modals.js @@ -54,30 +54,31 @@ $(() => { $(document).on("click.zf.trigger", (event) => { // Try to get the directly or find the closest parent - let $target = $(event.target); - if (!$target.is("a")) { - // Find the closest parent if the click is not directly on an - $target = $target.closest("a"); - } + const $target = $(event.target).closest("a"); // Check if an was found - if ($target.length) { - const dialogTarget = `#${$target.data("dialog-open")}`; - const redirectUrl = $target.data("redirectUrl"); - - if (dialogTarget && redirectUrl) { - $(""). - attr("id", "redirect_url"). - attr("name", "redirect_url"). - attr("value", redirectUrl). - appendTo(`${dialogTarget} form`); + if (!$target) { + return; + } + + + const dialogTarget = `#${$target.data("dialog-open")}`; + const redirectUrl = $target.data("redirectUrl"); - $(`${dialogTarget} a`).attr("href", (index, href) => { - const querystring = jQuery.param({"redirect_url": redirectUrl}); - return href + (href.match(/\?/) ? "&" : "?") + querystring; - }); - } + if (!dialogTarget || !redirectUrl) { + return; } + + $(""). + attr("id", "redirect_url"). + attr("name", "redirect_url"). + attr("value", redirectUrl). + appendTo(`${dialogTarget} form`); + + $(`${dialogTarget} a`).attr("href", (index, href) => { + const querystring = jQuery.param({"redirect_url": redirectUrl}); + return href + (href.match(/\?/) ? "&" : "?") + querystring; + }); }); $(document).on("closed.zf.reveal", (event) => {