Reply LinkBack Thread Tools Search this Thread
Old 11-07-2008, 04:44   #1 (permalink)
pagey
Resident Cheese Expert
 
pagey's Avatar
 
Join Date: Aug 2006
Location: South Wales
Posts: 702
Display an image in a div with javascript

Ok, I have a form which will be filled in by customers.

I want a small image to be displayed next to each textbox when the user has filled in the box. I have the validation sorted with javascript, but I would like an image to be displayed in a div depending on if they have filled in the correct information or not.

My JS isn't brilliant, but I'm thinking I'm going to need to use the getelementbyid thingy.

Something that basically says:

if blah blah = blah

document.getElementbyID.MyDiv = tick.gif

else

document.getElementbyID.MyDiv = cross.gif



You get the picture.

Any help would be appreciated!
  Reply With Quote
Old 11-07-2008, 07:53   #2 (permalink)
Pete Nice
Warchief
 
Pete Nice's Avatar
 
Join Date: Apr 2003
Posts: 445
Pagey - here's a little something I put together - you should be able to work it out from here:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> <script type="text/javascript"> function image() { var img = document.createElement("IMG"); img.src = "img/dir.gif"; document.getElementById('image').appendChild(img); } </script> </head> <body> <div id="image"></div> <div><a href="javascript:image();">click to see image</a></div> </body> </html>
  Reply With Quote
Old 11-07-2008, 08:58   #3 (permalink)
pagey
Resident Cheese Expert
 
pagey's Avatar
 
Join Date: Aug 2006
Location: South Wales
Posts: 702
Thanks Pete (again!)

I'm mega snowed under today, so when I come back to looking at the form, I'll have a play with that code.

Cheers
  Reply With Quote
Old 11-07-2008, 09:11   #4 (permalink)
blackf0rk
Enspiar Productions
 
blackf0rk's Avatar
 
Join Date: Oct 2006
Location: MKE, WI
Posts: 43
Pete Nice's code will work but doesn't do what I think you're trying to do pagey. In Pete's code, it simply adds an image element to the page. You basically want to visually show validation with graphics instead of text is a specific place and possibly replace the current graphic with a different one depending on validation. The correct syntax for getElementById is...

Code:
document.getElementById('idName')

If you already have an image next to each text box (with a different Id of course), you just need to change the image's src after the textbox has been run through your validation code and passess:

Code:
document.getElementById('idName').src='/images/tick.gif'

Simple as that.
  Reply With Quote
Old 11-07-2008, 09:46   #5 (permalink)
MikeMackay
Everything is fine.
 
MikeMackay's Avatar
 
Join Date: Feb 2005
Location: Witham & London
Posts: 815
Send a message via MSN to MikeMackay Send a message via Skype™ to MikeMackay
Another approach would be to have the image as the background and use JS to change the css class to .approved or something, where .approved is a style declaration in your site css.

Using that method could give you greater control over the form item without reverting to too much JS code.

Just my two cents/pence.....
__________________
  Reply With Quote
Old 11-07-2008, 09:47   #6 (permalink)
Pete Nice
Warchief
 
Pete Nice's Avatar
 
Join Date: Apr 2003
Posts: 445
really - there was me thinking that mine appended an image to the div id "image" rather than just adding it to the page:

Code:
document.getElementById('image').appendChild(img);

wassup blackf0rk? Can't read?....
  Reply With Quote
Old 11-07-2008, 10:25   #7 (permalink)
pagey
Resident Cheese Expert
 
pagey's Avatar
 
Join Date: Aug 2006
Location: South Wales
Posts: 702
Now, now gentlemen, lets not argue.

I'll be looking at this thing into more depth soon, so I'll be trying all of your suggestions to some degree I should imagine.

Thanks again.
  Reply With Quote
Old 11-07-2008, 11:07   #8 (permalink)
blackf0rk
Enspiar Productions
 
blackf0rk's Avatar
 
Join Date: Oct 2006
Location: MKE, WI
Posts: 43
Quote:
Originally Posted by Pete Nice
really - there was me thinking that mine appended an image to the div id "image" rather than just adding it to the page:

Code:
document.getElementById('image').appendChild(img);

wassup blackf0rk? Can't read?....

Wow. That's pretty harsh. And sad.

Chill out Pete. I guess it's a matter of semantics with you. Does your code not add an image to the page? Excuse me, let me rephrase it so you can understand...does your code not append an image to the div (which is on the page)?

At any rate, pagey isn't looking to keep appending images to divs. He's looking to swap out images based on validation. This is abundantly clear from his original post. If he was to use your code, he'd have swapping tick.gif's and cross.gif's appending one after each other on the page (or the div, whichever you'd like to say).

No one's knocking your code Pete. In fact, I learned from it. But it's the wrong code for the application. So get used to people trumping your code - it happens all the time; someone else always will have a "better" way of doing it. Look at me, MikeMackay trumped my code by posting his opinion on how he'd do it. And in my opinion it is a better way!
  Reply With Quote
Old 11-07-2008, 11:26   #9 (permalink)
Pete Nice
Warchief
 
Pete Nice's Avatar
 
Join Date: Apr 2003
Posts: 445
Like I said

Quote:
Pagey - here's a little something I put together - you should be able to work it out from here:

And btw, I don't care if someone trumps my code because I'm not a coder - I just like people to read what I said.
  Reply With Quote
Old 11-07-2008, 11:33   #10 (permalink)
Tom_E
Whippet Botherer™
 
Tom_E's Avatar
 
Join Date: Jan 2007
Location: Kent, England
Posts: 1,532
Don't argue with Pete – he's been drinking all day.
__________________
  Reply With Quote
Old 11-07-2008, 11:35   #11 (permalink)
Pete Nice
Warchief
 
Pete Nice's Avatar
 
Join Date: Apr 2003
Posts: 445
  Reply With Quote
Old 11-07-2008, 17:07   #12 (permalink)
wheedwacker
competitionmaster 2.0
 
wheedwacker's Avatar
 
Join Date: Oct 2006
Location: USA
Posts: 1,413
Personally, I would go with what MikeMackay said. But Pete's solution would work if you had just a function call that made the image appear only once when the right value was added.
__________________
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8