abdulwasay Posted December 14, 2021 Share Posted December 14, 2021 I want to get the attribute of li. but the issue is this that i am loading my page using ajax and after every 10 sec the list was updating. ajax was disabling my li i am not be able to click on any li. so i want to know how get the attribute of li. Quote Link to comment https://forums.phpfreaks.com/topic/314315-i-want-to-get-an-attribute-of-li/ Share on other sites More sharing options...
gizmola Posted December 14, 2021 Share Posted December 14, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/314315-i-want-to-get-an-attribute-of-li/#findComment-1592698 Share on other sites More sharing options...
abdulwasay Posted December 14, 2021 Author Share Posted December 14, 2021 //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>'; } Quote Link to comment https://forums.phpfreaks.com/topic/314315-i-want-to-get-an-attribute-of-li/#findComment-1592699 Share on other sites More sharing options...
gizmola Posted December 14, 2021 Share Posted December 14, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/314315-i-want-to-get-an-attribute-of-li/#findComment-1592700 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.