$("td").click(function() { $(this).empty(); } will work for table in html but not php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thatcollegeguy
    New Member
    • Nov 2009
    • 10

    $("td").click(function() { $(this).empty(); } will work for table in html but not php



    I am not sure what is wrong w/ this code. The main issue is that the table that is in the initial html will empty its td but the table that I load using php and jquery through a mysql database will not empty the td elements. There is a table underneath the button in the initial html. if you click on a box in the table, it will empty. the same does not happen for the table that is generated by the xml, php and mysql database. Please advise - my goal is to use a change html function in order to change an add button to a remove button and vice versa. If I cannot empty a table element, I won't be able to change the element. Thank you in advance everybody.


    -------------------------------------------------------------------
    Here is the html
    Code:
    <html>
    	<head>
    		<title>The HTML</title>
    		<script type="text/javascript" src="jquery-1.3.1.min.js"></script>
    		<script type="text/javascript" src="thejs.js"></script>
    	</head>
    	<body>
    	<div id="container"></div>
    		<input type="submit" id="getData" name="getData" value="Get Data!" />
    <table border="2"><tr><td>jkljkl</td><td>jkljl</td></tr></table>
    	</body>
    </html>
    Here is the XML
    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <people>
    	<person>
    		<firstname title="Mr">Steve</firstname>
    		<surname>Reynolds</surname>
    	</person>
    	<person>
    		<firstname title="Mr">David</firstname>
    		<surname>Grohl</surname>
    	</person>
    </people>
    Here is the php
    Code:
    <?php
    	include('dbc.php');
    	include('functions.php');
    
    /*
    Assumes you have connected to your database and that you have a table
    called "people" with 3 columns - title, firstname, surname
    */
    
    $query = "SELECT university,division,state FROM teamreports";
    $result = mysql_query($query);
    
    // create a new XML document
    $doc = new DomDocument('1.0');
    
    // create root node
    $root = $doc->createElement('teamreports');
    $root = $doc->appendChild($root);
    
    while($array = mysql_fetch_array($result)) {
    	
    	// add node for each row
    	$occ = $doc->createElement('team');
    	$occ = $root->appendChild($occ);
    
    	$child = $doc->createElement('university');
    	$child = $occ->appendChild($child);
    	$value = $doc->createTextNode($array['university']);
    	$value = $child->appendChild($value);
    
    	$child = $doc->createElement('state');
    	$child = $occ->appendChild($child);
    	$value = $doc->createTextNode($array['state']);
    	$value = $child->appendChild($value);
    	
    	$child = $doc->createElement('division');
    	$child = $occ->appendChild($child);
    	$value = $doc->createTextNode($array['division']);
    	$value = $child->appendChild($value);
    
    }
    
    // get completed xml document
    $xml_string = $doc->saveXML();
    
    header('Content-Type: application/xml; charset=ISO-8859-1');
    
    echo $xml_string;
    
    ?>


    Here is the javascript code
    Code:
    $(document).ready(function() {
    
    
    
    	$("#getData").click(function(){
    	
    	var data = "<table id='tbl' border='2'><tbody>";
    	
    		/* change the $.get to "thephp.php" when that is setup */
    	
    		$.get("thePHP.php", function(theXML){
    		
    				$('team',theXML).each(function(i){
    				
    					var division = $(this).find("division").text();
    					var university = $(this).find("university").text();
    					var state = $(this).find("state").text();
    					
    					data = data + "<tr><td>" + division + "</td><td>" + university + "</td><td> " + state + "</td><td class='addtolist'>Add To List</td></tr>";
    				
    				});
    				data = data + "</tbody></table>";
    				$("#container").html(data);
    		});
    	
    	});
    
    	$("td").click(function() {
              $(this).empty();
           });
    
    	
    });
  • DMsy2
    New Member
    • Dec 2009
    • 15

    #2
    I've never used JQuery itself but it seems likely that when you put the click event on "all the TD elements" it only does it for those which exist at that time.
    When getdata.click adds another table with new TD elements those new TD elements have no click event defined.
    How about putting the "set td.click" code inside the getdata.click function?

    And perhaps set on "#container td" so you only affect that table's elements?

    Comment

    • thatcollegeguy
      New Member
      • Nov 2009
      • 10

      #3
      I made your first suggested change. As you can see, the only difference is that now, you cannot empty the table below the button until the top table is generated after the button is clicked.

      Comment

      • DMsy2
        New Member
        • Dec 2009
        • 15

        #4
        I can't actually *see* anything on your PC :-)

        Re-writing innerHTML seems a bit of a minefield, in general.
        Try operating on #tbl and see it if it 'found' ???

        Comment

        • thatcollegeguy
          New Member
          • Nov 2009
          • 10

          #5
          quick question, are you able to view the webpage in question at http://smarterfootball.com/exits/theHTML.html ?

          Comment

          • thatcollegeguy
            New Member
            • Nov 2009
            • 10

            #6
            Kept the other change and changed the .js element to td #tbl as the id of the generated table is 'tbl', but didn't allow any td items to empty. So I changed the page back to how it was after my original change. It currently only empties after the submit button is clicked.

            Comment

            • DMsy2
              New Member
              • Dec 2009
              • 15

              #7
              Ah yes, I can see that page okay.

              I have no idea why it doesn't work.

              Comment

              • thatcollegeguy
                New Member
                • Nov 2009
                • 10

                #8
                Thanks for trying to help DMsy2

                The answer lies in the livequery plugin for jquery.

                First, I installed the plugin.
                Second, I inserted this code in the js file:

                Code:
                $('td') 
                    .livequery('click', function(event) { 
                    $(this).empty();
                    return false; 
                    });

                Comment

                • thatcollegeguy
                  New Member
                  • Nov 2009
                  • 10

                  #9
                  Another question: will using this xml technique have implications if the website has two people logged in at the same time?

                  Comment

                  Working...