Jump to content

[SOLVED] javascript/jquery POST to recognize retrieved elements...


joel24

Recommended Posts

I have a page which allows users to edit rows in the table,

When they click modify it uses a Jquery/javascript $.post function

This posts to a PHP page which sends back a form.

This all works fine, however, in the form I have a drop down menu where if certain values are selected I need different options to be shown/hidden - this works fine on elements that are on the page when it loads, but the jquery doesn't seem to recognize any of the elements in the retrieved form

 

Take for example the following:

 

HTML Page

 

<html>
<head>
<script type="text/javascript">
		jQuery(document).ready(function() {
		$('.editItem').click(function(event) {
				event.preventDefault();

				$.post("test.php", {}, 
					   function(data) {
						   if (data.length > 0) {
							   $('#testDiv').html(data);
						   } else {
							   $('#testDiv').html("<div align='center'>Error retrieving item details</div>");
						   }
					   });

		});

//test function for testButton
		$('.testButton').click(function(event) {
			event.preventDefault();
			alert('This test works!');
	  	});

});
</script>
</head>
<body>
<form id="test">
<button id="formRetrieval">Retrieve Form!</button>
<button class="testButton">Test Function!</button>
</form>
<div id="testDiv">
</div>
</body>
</html>

 

 

this is the php file the jquery posts to... test.php

<?php 
//just a dummy script to echo the form
echo '<form id="test">
<button class="testButton">Click me!</button>
</form>';
?>

 

 

In the above, if I've typed it all correctly, only the first TestFunction button will display an alert (the one on the HTML page).

When the 'Retrieve Form' button is clicked, it will load a new form into the testDiv and there will be a button with the same class 'testButton' (same as teh test function button), however, it will not display the alert.

 

If I had written a function, say, showAlert() which showed an alert, and set the button to onclick="showAlert()" then it would work.

 

In other words, the classes/id's of the elements which are being loaded through the PHP / post function aren't being identified...

 

Hope this all makes sense,

 

Any help would be greatly appreciated!

 

Link to comment
Share on other sites

You need to move you code so it binds after the new elements are added to the dom.

 

jQuery(document).ready(function() {
  $('.editItem').click(function(event) {
    event.preventDefault();
    $.post("test.php", {},
    function(data) {
      if (data.length > 0) {
        $('#testDiv').html(data);
        $('.testButton').click(function(event) {
          event.preventDefault();
          alert('This test works!');
        });
      } else {
        $('#testDiv').html("<div align='center'>Error retrieving item details</div>");
      }
    });
  });
});

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.