User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 426,140 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,698 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 2428 | Replies: 7
Reply
Join Date: Nov 2006
Location: Costa Rica
Posts: 65
Reputation: Racoon200 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Racoon200's Avatar
Racoon200 Racoon200 is offline Offline
Junior Poster in Training

Help change input's bg image with js

  #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?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,858
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 344
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: change input's bg image with js

  #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.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Join Date: Nov 2006
Location: Costa Rica
Posts: 65
Reputation: Racoon200 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Racoon200's Avatar
Racoon200 Racoon200 is offline Offline
Junior Poster in Training

Re: change input's bg image with js

  #3  
Jun 18th, 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  
Join Date: Jan 2007
Posts: 2,556
Reputation: MidiMagic is on a distinguished road 
Rep Power: 7
Solved Threads: 115
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Posting Maven

Re: change input's bg image with js

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

function changeSrc(clicked)
{
changeme.backgroundImage = 'someotherimage.someformat')
}

Put both the class and the id in the object with the background
<body class="someclass" id="changeme">
Daylight-saving time uses more gasoline
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,858
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 344
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: change input's bg image with js

  #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.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Join Date: Nov 2006
Location: Costa Rica
Posts: 65
Reputation: Racoon200 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Racoon200's Avatar
Racoon200 Racoon200 is offline Offline
Junior Poster in Training

Re: change input's bg image with js

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

Re: change input's bg image with js

  #7  
Jun 19th, 2007
and thank ya very much s.o.s, your always helping the needed.
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,858
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 344
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: change input's bg image with js

  #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.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 4:54 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC