Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

In my jQuery script I use the get function to extract data from a PHP page called "external.php". The data extracted is a simple path to an image (e.g. images/house.jpg). I need to use this path to specify the css value background-image of a div, whose class is "fullpage".

This is my faulty script: when I try to insert data in my script, the image doesn't appear.

$(document).ready(function(){
    $.get('external.php', function(data){
        $('.fullpage').css('background-image', 'url(' + data + ')');
    });
});

The script works if I simply switch "data" with the name of a variable whose value is my manually inserted path, so the problem is: how do I insert correctly the data value into the .css() method?

Thank you

share|improve this question
    
Are you sure that data is what you expect ? –  Ricardo Alvaro Lohmann Aug 13 '13 at 17:44
    
can you alert or log the data value ? and does your body tag have class="fullpage" attribute? –  Vedant Terkar Aug 13 '13 at 17:46
    
Looks like your code is fine: jsfiddle.net/D993y. I assume data doesn't contain what you expect. –  Jason P Aug 13 '13 at 17:46
    
use alert(data) to check what the page actually returns, it probably does not return the correct value as Ricardo said –  user2536474 Aug 13 '13 at 17:46
    
console.log(data) would be better. Alerts remove whitespace, which is sometimes the problem. –  Jason P Aug 13 '13 at 17:47

1 Answer 1

up vote 1 down vote accepted

Ricardo was right: I used console.log(data) as Jason P suggested and it showed me an html tag that didn't have to be in the php code. When I appended the result the tag didn't appear being part of an html structure, but logging helped me seeing it. Thank you!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.