How to pass id of li tag via AJAX when clicking on li element?

I want to pass in id value to show city function when li tag values ​​are clicked any acceptable method accepted

<script type = "text/javascript" >
  function showCity(str) {
    if (str == "") {
      document.getElementById("city").innerHTML = "";
      return;
    } else {
      if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
      } else {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
          document.getElementById("city").innerHTML = xmlhttp.responseText;
        }
      }

      xmlhttp.open("GET", "city.php?q=" + str, true);
      xmlhttp.send();

    }
  } 
</script>


<div id="menu">
  <ol id="test">

    <?php $stat=m ysql_query( "select * from `tms_state`"); while (($st=m ysql_fetch_array($stat))) { ?>

    <li onclick="showCity('this.value')">
      <?php echo "<option value='" . $st[ 'id'] . "'>" . $st[ 'name'] . "</option>";?>
    </li>

    <?php } ?>

  </ol>

</div>

      

I want to pass id value to show city function on click of li tag values.

+3


source to share


2 answers


<div id="menu">
    <ol id="test">

    <?php 
       $stat=m ysql_query( "select * from `tms_state`");
       while (($st=m ysql_fetch_array($stat))) { ?>

      <li onclick="showCity('<?php echo $st[ 'id'] ?>')">
          <?php echo "$st[ 'id']";?>
       </li>

    <?php } ?>

     </ol>

</div>

      



0


source


You can make it much easier with jQuery -



$(document).ready(function(){
    $('#test li').click(function () {
        var str = $(this).val();

        $.ajax(
            {
                url: "city.php?q=" + str,
                success: function (result) {
                    $("#city").html(result);
                }
            });
      });
 });

      

0


source







All Articles