jQuery in Action, 2nd edition*
The moose likes HTML, CSS and JavaScript and the fly likes Having basic javascript issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Having basic javascript issue" Watch "Having basic javascript issue" New topic
Author

Having basic javascript issue

Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

Hey there. So I have an html page that has this image (sorry I hadto cut off the < in the beginning and not put it between code tags, it was messing the image text up when I did that...):

img id="avatar_image" src="./images/SampleImage_Icon.png" width="128" height="128"/>

At the top of this html page in the header I have



So I can use various javascript files.

The view-core.js file does various things, one of which is to change the image from the code above when a function is called. Here is the relevant bit of code:


As you can see, right now, all i want to happen is for anytime one of these is called, it changes the image to lepr.gif

In the other js file - survey-logic.js, I have a way of dynamically generating text areas, radio buttons, etc, but for this example we will just use the text area. I want to change the image from above using onFocus() event.
So, in the survey-logic.js file I have this:



Which in my head, should call the Update_Avatar() function from view-core.js, and change the image from my html page to ./images/lepr.gif whenever I focus on the text area. But this is not happening. So I think i either have a syntax issue with how I am using
or
I am doing something wrong with this
Someone mind giving me an idea where I might be screwing up here please?


True wisdom is in knowing you know nothing - Socrates
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60780
    
  65

What errors are showing up in your Firebug log?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

Well first it was saying focus_event is undefined, so I changed that to "2" and it doesnt give me that error. Now it is saying avatar_image is undefinedm which I assume means I need something more than what I have here:

avatar_image.src="./images/lepr.gif";
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60780
    
  65

Is avatar_image defined?
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

avatar_image is the image on my html page. I am wanting to change the src within it to whatever I put in view-core.js.

So I think I am needing something like document.GetElementById("avatar_image.src")="./images/lepr.gif"; but that still isn't quite it. But avatar_image is from this:

img id="avatar_image" src="./images/SampleImagIcon.png" width="128" height="128"/>

And again, Im sorry I cant put that in to code tags but it cuts out most of the text if I do
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

If I use

GetElementById('avatar_image').src="./images/lepr.gif";

I get an error saying Object Expected
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60780
    
  65

You're flying blind if you don't have a good JavaScript reference at your side.

The method is document.getElementById.
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

I was just this second about to post that I figured out the problem....capital G instead of lower case g ><
It is always the small things eh?

This is the final code that worked:

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60780
    
  65

If you are sure that the image will exist on the page, you can vastly simplify that code to a single statement.
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

Yeah, i know how, I just was breaking it down further and further as i was trying to get it to work. But thanks for the tip, I will be doing so, I just wanted to show what I was going for in case anyone else sees this and it can help them.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Having basic javascript issue
 
Similar Threads
Ajax, Struts, and a few pesky event based issues
Is it possible to put a button inside autocomplete in jquery ui plugin?
unknown symbol on jsp page
Why always show the javascript class AutoSuggestControl is not defined?
making gif animation run once then go back to default image