开发者

how to get div's text to a variable

开发者 https://www.devze.com 2023-03-21 03:29 出处:网络
what I mainly want to do is to get the div\'s content and pass it in a variable. To explain what I have done until now :

what I mainly want to do is to get the div's content and pass it in a variable. To explain what I have done until now :

I have my php file that contains the code :

<?php
$connect = mysql_connect("localhost", "...","...") or die("Could not connect to the database.");
    mysql_select_db("...") or die("Could not find database <...>");

    $query = mysql_query("SELECT id FROM datainput WHERE id>=ALL(SELECT id FROM datainput)") or die("Query could not be executed");
    $row = mysql_fetch_assoc($query);

    echo开发者_开发知识库 $row['id'];
?>

In my index.php file I have written the following script :

 <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript">

            $("document").ready( function(){
                //setInterval("checkForNewData()", 1000);  //30 Minutes = 1800000 Milliseconds
                checkForNewData();
            });

            function checkForNewData(){
                $("#lastid").load("lastData.php");
                var mydata = $("#lastid").text();
            }  

        </script>

In my html tag I have a div with id="lastid". With the code below :

 var mydata = $("#lastid").text();

I want to keep the current text of lastid div, so I can later compare it with another.

As I have read here, this should have done mydata="6" (is the current result)? What am I doing wrong?

Can anyone help? Pleaseee...


You need to wait for the load to have finished. This is done by using a callback function like so:

   <script type="text/javascript">

        $("document").ready( function(){
            //setInterval("checkForNewData()", 1000);  //30 Minutes = 1800000 Milliseconds
            checkForNewData();
        });

        function checkForNewData(){
            $("#lastid").load("lastData.php", function(){
                var mydata = $("#lastid").text();
            });
        }  

    </script>

Please see the jQuery API docs for more information: http://api.jquery.com/load/

Essentially, the second argument of the load function can be a function. If it is, then whatever code is in that function will be executed when the load has completed.

0

精彩评论

暂无评论...
验证码 换一张
取 消