I am new to Javascript. I have set a couple of buttons on a page, like this:
<div id='MID_1_4'>
<div id="login">logout</div>
<button type="button" id="logout" onclick="loadXMLDoc2()">Logout</button>
</div>
...
I want to extract the id of the button who called loadXMLDoc2(). This method is defined as following:
function loadXMLDoc2() {
var retr = $(this).attr("id");
document.getElementById("L1_LEFT").innerHTML
= retr;
}
Yet, L1_LEFT is set to undefined when I click on buttons. I thought $(this) meant “the current HTML element”. If it is not the button then what is it? And how can I extract the button’s id?
thisrefers towindow(the global object) inside the function,thisrefers to theelementin theonclickhandler.while it’s bad approach to do javascript in html, try this:
A better approach is to separate HTML and javascript:
(Somewhere in js file )