views:

31

answers:

2

Hi,

I have 4 list items in my HTML and there is image and a text in the Item. What I am trying to do is when someone clicks on the list item, I want to pull the text out of it and use it to form an image url and assign it to an element's background. But this simple stuff is not working and firefox is throwing a warning without changing the image:

reference to undefined property g[m] : Line 69 {this error is shown 3 times}
Error in parsing value for background-image. Declaration Dropped.

Here's my code:

<ul id="colorChoice">
  <li class="setGuideColor"><img src="a.png">Red</li>
  <li class="setGuideColor"><img src="b.png">Green</li>
  <li class="setGuideColor"><img src="b.png">Blue</li>
  <li class="setGuideColor"><img src="d.png">Gray</li>
  <li class="setGuideColor"><img src="e.png">Yellow</li>
</ul>

These are the list items.

$(".setGuideColor").click(function(){
  var img = '"url(' + $(this).text() + '.png)"';
  alert(img);
  $(".myElement").css("background-image",img);
})

This is the JQuery part. Here in when I alert the variable img, I get a path enclosed in double quotes and the URL of the image generated is absolutely right. I aint getting what is the issue.

+2  A: 

A proper value for background-image would be:

url(somePath.png)

Where you seem to have:

"url(somePath.png)"

Lose the outer quotes.

thenduks
Thanks for your replay but If you see the image url, its: "url(somepath.png)"I think having quotes around the URL is necessary.
Meher Ranjan
No, you don't want those quotes. Here's some valid css: `background: white url(abc.png) no-repeat center center;`, and here's some invalid css: `background: white "url(abc.png)" no-repeat center center;`. The latter is what you've got, the former is what you need :)
thenduks
Hi theduks, I am sorry, Your solution worked partially. It changed the image but firefox throws the warning again: reference to undefined preoperty. :(
Meher Ranjan
Changed the image how? Pls paste updated code.
thenduks
By removing the quotes enclosing the URL :). But firefox is still throwing the -reference to undefined property- warning. Any Idea?Also I would like to tell you that I have the JAVASCRIPT STRICT OPTIONS in firefox set to true.
Meher Ranjan
You've probably removed the wrong quotes. Here's the function you want for your click handler: `function() { $('.myElement').css('background-image', 'url(' + $(this).text() + '.png)' ); }`
thenduks
no no, I removed the proper quotes. The warning I get is shown for the jQuery Line 69.
Meher Ranjan
Ok, can you paste your updated code?
thenduks
I have posted my updated code.
Meher Ranjan
A: 
$("#ColorOption").append('<ul id="colorChoice">
  <li class="setGuideColor"><img src="cyan.png">Cyan</li>
  <li class="setGuideColor"><img src="magenta.png">Magenta</li>
  <li class="setGuideColor"><img src="yellow.png">Yellow</li>
  <li class="setGuideColor"><img src="black.png">Black</li>
 </ul>');


$(".setGuideColor").click(function(){
  $(".drag-y").css("background-image",'url(' + $(this).text() + '.png)');
})

Hi thenduks , here's the original code.

Meher Ranjan
Ok, well there's no problem with the line inside the click handler now. When does your 'reference to undefined property' error occur exactly? On page load? On click of one of the li's?
thenduks
Also for future reference you shouldn't 'answer' your own question with an update like this. Better to simply update your question above with: *Update: ...* or the like. That way I can update my answer and things are sane instead of making a mess for future googlers to stumble into :)
thenduks
I see the warning as soon as I click(before the background-image changes). It is shown 3 times.
Meher Ranjan
You have to put backslash `\\` in the end of each line of multi-line string to make this script work.
pepkin88
Thanks for your suggestion but I have put line breaks here, so that the code is readable. :)
Meher Ranjan