Get ID of LI Element

Use pure Javascript to get the id value of a selected li element.

by Alex Azuero

HTML

<ul id="actBtn">
    <li id="123">
        <button>AB12345</button>
    </li>
    <li id="321">
        <button>BG12345</button>
    </li>
    <li id="789">
        <button>DG12345</button>
    </li>
    <li id="987">
        <button>EQ12345</button>
    </li>
</ul>
<input type="hidden" id="accountNum">
<input type="submit" value="submit" onClick="getAcc()">

JavaScript

$(document).ready(function () {
    $("ul[id*=actBtn] li").click(function() {
        alert($(this).html()); //gets the innerHTML of clicked li
        alert($(this).text()); //gets the text contents of clicked li
        var a = $(this).attr('id'); 
        alert(a);
        $('#accountNum').val(a);
    });
});

function getAcc() {
    alert("got here");
    var acc = document.getElementById("accountNum").value;
    alert (acc);
}