2

I am having problem with the following line:

document.getElementById("giftcard").getElementsByTagName("label").style.backgroundImage ="url(giftcard_icon-d.jpg)";

I am trying to change background image after clicking a label.

I do have 5 labels in a page, but when i click on label, the background should change it. Other labels should be reset to use this giftcard_icon-d.jpg image as their background.

How can I fix this?

$(document).ready(function() {
    $('#giftcard label').click(function() {
       document.getElementById("giftcard").getElementsByTagName("label").style.backgroundImage ="url(giftcard_icon-d.jpg)";
       this.style.backgroundImage ="url(giftcard_icon-a.jpg)";      
}); 
1
  • What is happening exactly? Background changes for none of them??? Commented Apr 27, 2011 at 20:27

3 Answers 3

4

If you're using jQuery, this ought to do the trick:

$(document).ready(function() {
    $('#giftcard label').click(function() {
        $("#giftcard label").css("background-image", "url(giftcard_icon-a.jpg)");
    });
});

But it also depends on whether the ID is correct, a label is present and that the image URL is valid.

For multiple label's you should be OK with a selector:

$("#giftcard > label").css("background-image", "url(giftcard_icon-a.jpg)");

To update after concluding exactly what it was you wanted:

$('#giftcard label').click(function() {
    $("#giftcard label").css("background-image", "url(giftcard_icon-d.jpg)");
    $(this).css("background-image", "url(giftcard_icon-a.jpg)");        
});
Sign up to request clarification or add additional context in comments.

2 Comments

Perfect! it works now. I used this way : '$('#giftcard label').click(function() { $("#giftcard label").css("background-image", "url(giftcard_icon-d.jpg)"); this.style.backgroundImage ="url(giftcard_icon-a.jpg)"; }); '
Yeah, I worked out properly what you wanted after I posted - I'll add it to the answer.
2

Firstly your mixing vanilla javascript and jquery here, seems a bit silly. If you want to alter the other labels to "reset" them you can do something like this:

 $(document).ready(function() {

  $('#giftcard').delegate('label', 'click', function() {

    $(this).css('background', 'url(giftcard_icon-d.jpg)');

    $('#giftcard > label').not(this)
     .css('background', 'url(giftcard_icon-a.jpg)');

  }); 

});

fiddle: http://jsfiddle.net/garreh/GUFtx/

Comments

1

If I understood it correctly this should do the trick

$(function() {
    $('#giftcard label').click(function() {
        $('#giftcard label').css("background-image", "url(giftcard_icon-d.jpg)");
        $(this).css("background-image", "url(giftcard_icon-a.jpg)");
    });
});

working sample http://jsfiddle.net/FvNdR/

1 Comment

@XML guy I Guess your doubt was just on how to set the style properties on jquery, when @Mr. Disappointment posted his answer you got the idea and solved by yourself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.