1

i want to change my button background image using javascript so i wrote a code like this but seems to be it's not working well.

$('#search-button-input').css('background-image', $.format("url('Images/%(color)s.png')", { color: e }));

e is image comes with fire the some other function.

my input tag like this

<input id="search-button-input" type="button" name="q" class="search-button-input"  value="search" />
Cœur
  • 37,241
  • 25
  • 195
  • 267
Gayan
  • 2,750
  • 5
  • 47
  • 88
  • What does `$.format` do? Edit: obviously it is supposed to subsitute the value into the string, but what does the call *actually* do? – lonesomeday Jul 09 '11 at 13:43
  • it's uses for string concatenation like in c# does string.format('{0}/{2}',one,two); :) – Gayan Jul 09 '11 at 13:50
  • @lonesomeday For reference: this is the [jQuery Format](http://jquery.bassistance.de/api-browser/plugins.html#jQueryformatStringArrayString) plugin. @Gayan Can't you use the `+` operator for concatenation? – Félix Saparelli Jul 09 '11 at 13:50
  • Yes, evidently. This is likely the bit that isn't working though. If you put `console.log($.format("url('Images/%(color)s.png')", { color: e }))` immediately above the line you have there, what is logged? – lonesomeday Jul 09 '11 at 13:51
  • it's giving me a output which i expected. – Gayan Jul 09 '11 at 13:53
  • @Gayan OK, can you define "not working well" please? Could you provide a live example of your code? – lonesomeday Jul 09 '11 at 13:56
  • possible duplicate of [Switching a DIV background image with jQuery](http://stackoverflow.com/questions/253689/switching-a-div-background-image-with-jquery) – bdonlan Jul 09 '11 at 15:08
  • Here you have: http://stackoverflow.com/questions/253689/switching-a-div-background-image-with-jquery Hope this helps. Cheers – Edgar Villegas Alvarado Jul 09 '11 at 13:52

1 Answers1

-1

Well, one way to do it is if you're using this function to respond to a mouseover event, an easier way to do it is with CSS itself. In CSS you can specify the style of something which is being hovered over like this:

element:hover { ... }

If this isn't what you want, then you can do something like this for example:

element.setAttribute("style", "color: blue; background-color: red");
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Sean
  • 54
  • 6
  • no luck it's not working for me and i'm suppose to use this for mover hover image change. thanks for reply – Gayan Jul 09 '11 at 13:59