Jquery added by ajax request in php page doesn't work

i have a script jquery that upload an image and show it by a php script called in this way:

<script type="text/javascript" >
 $(document).ready(function() { 
 $('#photoimg').live('change', function(){ 
   $("#imageform").ajaxForm({target:'#logo'}).submit();
});
}); 
</script>

where photoimg is the input type file and image form the form. logo is the div where image will be.

Now when image is loaded in div whit php script in this way:

echo "<img src='".$actual_image_name."' id='imm' style=\"width:500;height:350;\" \>\n";

i have to add a jquery script to drag image inside the div to "center" it. if i use echo after the line before in this way:

echo '<script type="text/javascript">'; 
echo "$(function(){";
echo "var maskWidth  = $(\"#logo\").width();";
echo "var maskHeight = $(\"#logo\").height();";
echo "var imgPos = $(\"#imm\").offset();";
echo "var imgWidth = $(\"#imm\").width();";
echo "var imgHeight  = $(\"#imm\").height();";
echo "var x1 = (imgPos.left + maskWidth) - imgWidth;";
echo "var y1 = (imgPos.top + maskHeight) - imgHeight;";
echo "var x2 = imgPos.left;";
echo "var y2 = imgPos.top;";
echo "$(\"#imm\").css({top: 0, left: 0, cursor: 'move'});";
echo "$(\"#imm\").draggable({ containment: [x1,y1,x2,y2] });";
echo "});\n";
echo "</script>\n";

it doesn't work. can you help me pls?

Answers


You should never want to execute Javascript from an external source like this. Just put the code into the calling page/script and execute it after the Ajax content has been added.


Need Your Help

Greasemonkey script to reorder page sections that are split only by HTML comments?

javascript greasemonkey userscripts html

I have been trying to change the order of elements in this code so the Account Information, Statistics and Website Traffic dividers (in that order) are listed before the jump-search divider.

jQuery scroll a div up and down using two buttons

jquery

I have a simple set of two buttons that when hovered should make a div move up and down to simulate a scrolling effect:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.