The simplest solution is simply not to pass the ID, but the entire element itself into the function. You can then retrieve the DOM node's ID within the function itself, so that you don't have to query the DOM again:
function clickTab(el) {
el.className = " active";
// Get the ID
var id = el.id;
console.log(id);
}
.active {
background-color:red;
}
<div id="communitytab" onClick="clickTab(this)">Interesting community content</div>
However, you should really avoid using inline JS. Instead, use Element.addEventListener
instead, which will pass the DOM node as the contextual this
. Also, you can use classList
instead of className
(it is actually very widely supported now, ~98% of all browser traffic), because the API allows you to add/remove classes easily using built-in prototypes, e.g. el.classList.add('active')
:
function clickTab() {
var el = this;
el.classList.add('active');
// Get the ID
var id = el.id;
console.log(id);
}
document.getElementById('communitytab').addEventListener('click', clickTab);
.active {
background-color:red;
}
<div id="communitytab">Interesting community content</div>