File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Having basic javascript issue

 
Matt Kohanek
Village Idiot
Ranch Hand
Posts: 484
Java jQuery Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What errors are showing up in your Firebug log?
 
Matt Kohanek
Village Idiot
Ranch Hand
Posts: 484
Java jQuery Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is avatar_image defined?
 
Matt Kohanek
Village Idiot
Ranch Hand
Posts: 484
Java jQuery Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 484
Java jQuery Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I use

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

I get an error saying Object Expected
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 484
Java jQuery Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 484
Java jQuery Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic