Q:

how to get the parent element id in JQuery or Javascript

i have un ordered list of html elements from the list on clicking any of the list i want to get the parent ul tag id.
       <ul id="123">
             <li>
               <li>test1</li>
                 <a href="#" onclick="validate()"/>
                </li>
             </li>
              ........
              ........
         </ul>

function validate(){
         var test=$(this).parent().parent().attr('id');
            alert(test);
          }

above validate function test variable result should ul tag id that is 123.

A:

First problem is that your function validate() dont know what this refers to, so you need to add it like validate(this);
Then we can do it like this.
function validate(obj) {
  var test = $(obj).closest("ul").attr('id');
  alert(test);
}

Also, you should not really have <li> as a child of another <li> use something like this structure.
<ul id="123">
  <li>
    <a href="#" onclick="validate(this)">test1</a>
    <ul id="456">
      <li>
        <a href="#" onclick="validate(this)">test2</a>
      </li>
    </ul>
  </li>
</ul>

function validate(obj) {
  var test = $(obj).closest("ul").attr('id');
  alert(test);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id="123">
  <li>
    <a href="#" onclick="validate(this)">test1</a>

    <ul id="456">
      <li>
        <a href="#" onclick="validate(this)">test2</a>
      </li>
    </ul>
  </li>
</ul>

