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" val="test123">
        <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 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);
        if (a="123"){
         a= "test123";
         alert(a)
        }else {alert(a)};
       
        $('#accountNum').val(a);
       // getAcc();
    });
});

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