views:

114

answers:

2

I am having an issue with setting the background-image of a #x to the background image of #y.

Correct me if I am wrong but I think you would do this in a simple case by saying

$('#x').css('background-image',$('#y').css('background-image'));

The problem is that when y was initially set up, it was set up like this:

<div id='y' style='background-image: url(./randomcolor.php?ibase=<?php echo $entity->name; ?>) ....

So when it does it, it is getting the URL of the generator, and javascript is just regarding that as a string. Do I have to do an ajax get here, or is there some kind of eval I could use.

+1  A: 

Your code looks fine.

The second part of your question doesn't make sense to me... Are you saying that your #x div ends up with a background image url of ./randomcolor.php?ibase=<?php echo $entity->name; ?> rather than the ./randomcolor.php?ibase=xyz that you'd expect. If so, you've got something going wrong with your PHP code. JQuery is just JavaScript, so it will only ever be dealing with client-side rendered HTML, if that HTML contains php tags, then the server side rendering of your page isn't doing its job.

Alconja
no I'm not saying that the php code gets out where it's not supposed to be: I was just showing how the image gets set. On the clientside it ends up with actually the full URL:http://blabla/randomcolor.php?ibase=xyz It is not setting the background of the #x div to the background of the #y div or anything at all. So I assume it's not calling the server function.
Alconja
I accepted this answer because it asks very good questions even though the solution lies elsewhere. The jquery forum answer was not exactly correct (actually both forms background-image and the camelcase version DO work) but in messing around I discovered the error which was not really related to any of this and honestly I'd report it if I could remember it.
+1  A: 

http://forum.jquery.com/user/rickvhoeij provided the clue to answer this in the jquery forum. http://forum.jquery.com/topic/setting-background-image-to-another-div-s-dynamically-generated-image Actually the code isn't fine, when I changed background-image to camelCase (backgroundImage) then it started working. This is the kind bug I can never eyeball!! It just goes by, we are all so used to seeing that it's so easy to miss. Alconja, I appreciate your helpful probe questions. The answers would have been yes, the graphic displays OK in the first place, and everything else copies fine except that. and now I also realize that Firefox is not always Firefox. I started work on this in Linux and I moved to a Mac and Firebug looks a little different here. I assumed there weren't errors because FB wasn't flagging them the same way it does on Linux.

Duh! We are visual creatures of habit.

Well that's odd... There's obviously something else subtle going on here because `.css('background-image')` and `.css('backgroundImage')` should actually *both* work (see http://api.jquery.com/css/), so I think you've stumbled on some obscure bug in jquery (or the browser) that causes the hyphen version to fail but the camelCase version to work in this specific instance (I wasn't able to replicate it - it worked for me using `background-image`). Either way, glad you got it working. :)
Alconja
Actually, Alconja you are right, background-image is not wrong because it works other places in my code where I just have static images. There was some normal bug which was way easier to fix, but like I said the problem was I just wasn't used to the display in the Mac platform. and I'm sorry but I can't remember exactly what the bug was cuz it was like 3 days ago.