$('.tabLabel').click(function() {
if (!$(this).hasClass('activeTab')) {
$('.tabLabel').removeClass('activeTab');
$(this).addClass('activeTab');
$('.tab').toggleClass('activeTabContent');
}
});
var tabLabels = document.querySelectorAll('.tabLabel');
Array.from(tabLabels).forEach(function(tabLabel) {
tabLabel.addEventListener('click', function(e) {
var activeTabLabel = e.target.classList.contains("activeTab");
if (!activeTabLabel) {
tabLabel.classList.remove('activeTab');
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="tabLabel activeTab">
<p>Tab One</p>
</div>
<div class="tabLabel">
<p>Tab Two</p>
</div>
</div>
I have this click function that I'm getting a bit lost in converting to "Vanilla" Js (plain js). Any help would be appreciated. I've tried so far what I can but the lexical this is what is confusing me.
UPDATE: I'm still having troubles but I can at least console.log the elements that I want to target.
// Working Code in jQuery
$('.tabLabel').click(function() {
if (!$(this).hasClass('activeTab')) {
$('.tabLabel').removeClass('activeTab');
$(this).addClass('activeTab');
$('.tab').toggleClass('activeTabContent');
}
});
// My attempt at converting
// Does not work. returns err => Cannot read property 'remove' of undefined || Cannot read property 'toggle' of undefined
var tabLabels = document.querySelectorAll('.tabLabel');
var tab = docuement.querySelectorAll('.tab');
Array.from(tabLabels).forEach(function(tabLabel) {
tabLabel.addEventListener('click', function(e) {
if (!this.classList.contains('activeTab')) {
tabLabel.classList.remove('activeTab');
this.classList.add('activeTab');
tab.classList.toggle('activeTabContent');
}
});
});