1

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

putvande
  • 15,068
  • 3
  • 34
  • 50
user1190547
  • 51
  • 1
  • 6
  • Are you sure that `data` is what you expect ? – Ricardo Alvaro Lohmann Aug 13 '13 at 17:44
  • Looks like your code is fine: http://jsfiddle.net/D993y/. I assume `data` doesn't contain what you expect. – Jason P Aug 13 '13 at 17:46
  • 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
  • use alert(data) to check what the page actually returns, it probably does not return the correct value as Ricardo said –  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
  • I tried appending the value of data to the body (just to see what came up) and it gives the correct value, that is a path like in the example above.If i just copy and paste this path in the right position my code works, the same if i create a variable whose value is the pasted path. Also there is a div with a fullpage attribute. – user1190547 Aug 13 '13 at 17:52
  • What is the css of div previously ? does it contain some data or not ? – Vedant Terkar Aug 13 '13 at 17:55
  • Have you tried inspecting the element after the script runs? What css does it have? Is the path correct? – Jason P Aug 13 '13 at 17:59

1 Answers1

1

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!

user1190547
  • 51
  • 1
  • 6