I have defined an on-click function on my button. I need my HTML element class name while clicking on this element. if my HTML button is like:
<button type="button" class="nav-item" onclick="ButtonClick()">Click</button>
How can I get class name to click button.
function ButtonClick() { console.log("Class Name"); }
Add “this” in the on-click function.
<button type="button" class="nav-item" onclick="ButtonClick(this)">Click</button>
Apply this code to get the element class name.
function ButtonClick(obj) { var myClass = $(obj).attr('class') if(myClass == "nav-item" ) { console.log("Class name found.") }else { console.log("Class name not found.") } }
Question and answer is powered by AnsPress.io