N
N
nextnull2019-04-12 23:16:27
1C-Bitrix
nextnull, 2019-04-12 23:16:27

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

1 answer(s)
K
Kudis, 2019-04-15
@nextnull

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'));
    });
});

or in pure js
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);
                }
            });
        });
    }
});

Just change the arrow functions to oldstyle if you want it to work in ancient explorers

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question