views:

107

answers:

3
var req = function () {
                $.ajax({
                    url: "out.php",
                    cache: false,
                    success: function(html){
                        $("#stuff").empty().append(html);
                    },
                    complete: function(){
                        req();
                    }

                    });
            };
            req();

This spinet above gets the output in the following format: 0,0

How can I parse that and have one number placed in the css selector top and the other in left?

+2  A: 

Well, if I understood your php output is somwething like x,y and you need x to be the css attribute top and y the css attribute left of an element. In my example I will call the element which will hold these attributes with the id elem.

So here it goes:

var css_attr = req.split(",");
$('#elem').css('top', css_attr[0]).css('left', css_attr[1]);

If you'd like to do it just in succes of AJAX call just change var css_attr to var css_attr = html.split(","); and the rest stays the same.

That should be all. The split() function is pretty much equal to php's explode().

Enjoy!

Bogdan Constantinescu
A: 

Sorry browser restarted and I don't have an account. I noticed that the unit applied to the number is pt, how do I make that pixels?

Just add "px" to the values. For example, in Bogdan's answer, use `css_attr[0] + "px"` instead of `css_attr[0]`.
Ben Blank
+1  A: 
var css_attr = req.split(",");
$('#elem').css({
  top: css_attr[0]+'px',
  left: css_attr[1]+'px'
});
gnarf