Jump to content

I want to get an attribute of li.


abdulwasay

Recommended Posts

With no code to look at, there is nothing anyone can do to help you.  A page where you are updating the contents every 10 seconds, and in the process blocking any interactivity, doesn't seem like a very good design to me.  At very least, you should be checking to see if an update is even needed or relevant.

Link to comment
Share on other sites

//Index.php
<ul id="">
    <div id="carlist">
    </div>
</ul>

//script
<script>

   $(document).ready(function () {
var listtt = setInterval(function () {
    $.ajax({
        url: "list.php",
        success: function (html) {
            let listtt = html;
            if (listtt) {
                document.getElementById("carlist").innerHTML = listtt;
            }
        }   });

}   , 5000)

    $('.a').click(function () {
        carId = $(this).attr("abc");
        alert (carId);
   });
});</script>

//list.php
 $query = mysqli_query($con, "SELECT *  FROM tblproduct");
    while($data = mysqli_fetch_assoc($query))
    {
          print '<li class="get_car" car_id="'. $data['id'].'"><span>'.$data['name'].'</span><button class="a" abc="'. $data['id'].'" type="button">hello</button></li>';
    }

 

Link to comment
Share on other sites

In the future, please use the <> button to insert code.  I fixed it for you this time.

  • You stated your timer was every 10 seconds, but your example shows it's 5
    • You should note that if you test it with something longer like 30000, after the initial render, the links don't work, so it's not a timing issue
  • I don't know how many rows you are sending but it seems pretty inefficient to blow out the entire list every 5 seconds.
  • The main issue I see is that you are only setting the click handler when the page loads.  Once you reload the page,  the reloaded li's won't have click handlers attached.  

So the simplest fix is to move the code that sets the click handlers inside the setInterval code. 

With that said, the update might perform faster if you just write your click handler function, and then have your button markup include the onclick="clickHandlerFunction()".  This way the DOM doesn't have to process adding the click handler for every refresh.  

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.