Answer the question
In order to leave comments, you need to log in
How to make a link add to compare AJAX?
Hi guys, tell me how to make a link to add a product to the comparison.
checkbox done, but how about a href? Help
<?
$iblockid = $arElement['IBLOCK_ID'];
$id=$arElement['ID'];
if(isset($_SESSION["CATALOG_COMPARE_LIST"][$iblockid]["ITEMS"][$id]))
{
$checked='checked';
}
else
{
$checked='';
}
?>
<input <?=$checked;?> type="checkbox"class="compare" id="compareid_<?=$arElement['ID'];?>" onchange="compare_tov(<?=$arElement['ID'];?>);">
Answer the question
In order to leave comments, you need to log in
If your code works, i.e. function compare_tov(id);
works, then:
do:
hang event
on jQuery
$(() => {
$('.to-compare').on('click', event => {
event.preventDefault();
compare_tov($(this).data('id'));
});
});
document.addEventListener("DOMContentLoaded", () => {
let compareLinks = document.querySelectorAll('.to-compare');
if (compareLinks.length) {
compareLinks.forEach(link => {
link.addEventListener('click', event => {
event.preventDefault();
let itemId = event.dataset.id;
if (itemId.length) {
compare_tov(itemId);
}
});
});
}
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question