| Home | Register | FAQ | Members List | Search | Today's Posts | Mark Forums Read |
|
|
#1 (permalink) |
|
Resident Cheese Expert
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! |
|
|
|
|
|
#2 (permalink) |
|
Warchief
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:
|
|
|
|
#4 (permalink) |
|
Enspiar Productions
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:
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:
Simple as that. |
|
|
|
#5 (permalink) |
|
Everything is fine.
|
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..... |
|
|
|
#8 (permalink) | |
|
Enspiar Productions
Join Date: Oct 2006
Location: MKE, WI
Posts: 43
|
Quote:
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! |
|
|
|
|
#9 (permalink) | |
|
Warchief
Join Date: Apr 2003
Posts: 445
|
Like I said Quote:
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. |
|
|
|
|
#12 (permalink) |
|
competitionmaster 2.0
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. |
|
![]() |