change input's bg image with js

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Nov 2006
Posts: 66
Reputation: Racoon200 is an unknown quantity at this point 
Solved Threads: 1
Racoon200's Avatar
Racoon200 Racoon200 is offline Offline
Junior Poster in Training

change input's bg image with js

 
0
  #1
Jun 17th, 2007
Hi. I have an input with a background image (done in css);
I want to change the background image with the click of a button.

How do it do it?
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,614
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 466
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: change input's bg image with js

 
0
  #2
Jun 17th, 2007
Post some code so we can easily help you out without rewriting the same thing over again.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 66
Reputation: Racoon200 is an unknown quantity at this point 
Solved Threads: 1
Racoon200's Avatar
Racoon200 Racoon200 is offline Offline
Junior Poster in Training

Re: change input's bg image with js

 
0
  #3
Jun 19th, 2007
this is the input...
<input blah blah blah class='someclasss' onclick='changeSrc(this)'>

.someclass
{
background-image:url(someimage.someformat);
}

function changeSrc(clicked)
{
clicked.style.backgroundImage = 'someotherimage.someformat' //this won't work in IE7!
}

Manipulate the code freely. thanks
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 3,203
Reputation: MidiMagic has a spectacular aura about MidiMagic has a spectacular aura about 
Solved Threads: 164
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Nearly a Senior Poster

Re: change input's bg image with js

 
1
  #4
Jun 19th, 2007
You need an id on the same object with someclass. That id becomes the target of the assignment statement:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function changeSrc(clicked)
  2. {
  3. changeme.backgroundImage = 'someotherimage.someformat')
  4. }

Put both the class and the id in the object with the background
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <body class="someclass" id="changeme">
Daylight-saving time uses more gasoline
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,614
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 466
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: change input's bg image with js

 
0
  #5
Jun 19th, 2007
function changeSrc(clicked)
{
clicked.style.backgroundImage = 'someotherimage.someformat' //this won't work in IE7!
}
Try this:
  1. function changeSrc(clicked)
  2. {
  3. clicked.style.backgroundImage = "url(imageName)";
  4. //the above stmt assumes that the image is present
  5. //in the same path as that of the html file
  6. }
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 66
Reputation: Racoon200 is an unknown quantity at this point 
Solved Threads: 1
Racoon200's Avatar
Racoon200 Racoon200 is offline Offline
Junior Poster in Training

Re: change input's bg image with js

 
0
  #6
Jun 19th, 2007
and on purpose, anybody here knows a free js editor with line numbers and autocomplete?
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 66
Reputation: Racoon200 is an unknown quantity at this point 
Solved Threads: 1
Racoon200's Avatar
Racoon200 Racoon200 is offline Offline
Junior Poster in Training

Re: change input's bg image with js

 
1
  #7
Jun 19th, 2007
and thank ya very much s.o.s, your always helping the needed.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,614
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 466
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: change input's bg image with js

 
0
  #8
Jun 20th, 2007
> and on purpose, anybody here knows a free js editor with line numbers and autocomplete?
Yes, you can find it here. For its tutorials, see here.

> and thank ya very much s.o.s, your always helping the needed.
You are welcome, fellow Javascripter. ;-)
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC