2

How to get image size in an ajax callback? it returns value 0.

index.php

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $('#click').click(function(){
        $.ajax({
        url: "result.php", 
        dataType: "html",
        type: 'POST', 
        data: "search=robot", 
            success: function(images){ 
                $("#result").html(images);
                var width = $('#images').width();
                alert(width);
            }
        });
    });
});
</script>
<body>
<a id="click">click</a>
<div id="result"></div>
</body>

result.php

<img src="http://gandt.blogs.brynmawr.edu/files/2009/01/<?php echo $_POST['search']; ?>.jpg" id="images" />

1 Answer 1

1

Your Success should look like this:

  success: function(images){ 
        $("#result").html(images);
        var width = $('#result img').width();
        alert(width);
    }

You need to get the image element from the result div. There is no element with the id of #images

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.