Weyooo
Weyooo

Reputation: 26

JQuery Can't Detect Result of PHP Echo

While working with JQuery and PHP, I encountered the following problem:

There is a DIV with a class of "Drag" which I use twice, once hard-coded in HTML, like this:

<div class='Drag'></div>

Subsquently, PHP generates this DIV within the same page, as follows:

echo "<div class='Drag'></div>";

The PHP code executes due to an AJAX call, so maybe this is the source of the problem?

As a result, the page contains these two DIVs. Also, there is jQuery code, as follows:

$(".Drag").draggable();

But, the jQuery code will only work for the DIV which was coded in HTML and not for the one which was generated by PHP. Why?

Upvotes: 0

Views: 76

Answers (2)

aknosis
aknosis

Reputation: 4348

You need to run $(".Drag").draggable() again after the ajax request is complete.

When you first call $(".Drag").draggable() the only div that is on the page is the one that is created in html.

Upvotes: 2

Harlan Wilton
Harlan Wilton

Reputation: 504

Either your running the javascript before the DOM is finished loading and therefore before the second div is within context OR the jQuery library isn't setup to iterate over the selector.

The code to possibly resolve your issue

$(document).ready(function() {
  $('.Drag').each(function() {
     $(this).draggable();
  });
});

Upvotes: 0

Related Questions