Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MyirLik's avatar

Hide the div if the date is the same on two divs

<button type="button" class="btn btn-default color_green" data-container="body" data-toggle="popover" data-placement="bottom" data-content="23-10-2020" data-original-title="" title="" style="display: inline;">
                                                    Something
                                                </button>
<button type="button" class="btn btn-default color_green" data-container="body" data-toggle="popover" data-placement="bottom" data-content="23-10-2020" data-original-title="" title="" style="display: inline;">
                                                    Something
                                                </button>

Hi, I have this button for example, and I would like to take over in a variable after data-content = "23-10-2020" for example and hide if one if two dates are of the same type

<script>
    let divs = document.getElementsByClassName('btn btn-default color_green');
    for(let i = 0; i < divs.length; i++){
        console.log(i);
        if(i % 2 === 0) {
            divs[i].style.display = 'inline';
        } else {
            //divs[i].remove();
            divs[i].style.display = 'none';
           
        }
        
    }
</script>

I had tried to do this if there are two buttons, but I would like to date, how can I proceed?

0 likes
1 reply
oranges13's avatar

How you accomplish this depends on if you want to compare the two strings to see if they're the same, or parse them into dates and see if they're the same. For example, if you're comparing strings, you could have "23-10-2020" (european format) and "10-23-2020" (american format) which are technically the same date, but unless you parse them as dates they won't be seen as equal, so keep that in mind.

A very basic example for you

let divs = document.getElementsByClassName('btn btn-default color_green');
let seen_dates = array();
divs.forEach(function(div))
	if (seen_dates.includes(div.dataset.content)) {
		div.style.display = 'none';
	} else {
		div.style.display = 'inline';
	}
    // Save any dates we have seen to compare. If we see the same date again, hide that div.
    seen_dates[] = div.dataset.content;
}

Please or to participate in this conversation.