File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes is null question 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 "is null question" Watch "is null question" New topic
Author

is null question

Ryan Christiani
Ranch Hand

Joined: May 15, 2011
Posts: 30
Question.
I have a document, with an unordered list and some list items. List id is images, list items, are id'ed img1, img2...etc

I want on a click of an image, to display some text from an array.

In my colImg function I want to cycle though the list array, and then send that number to the detailWrite function, but imgNum comes up as null?




What am I doing wrong here?

Thank


Probably going to ask a lot of possibly stupid questions...but how else will I learn? am I right!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61767
    
  67

Explain: document.getElementById("images").length

What are you expecting this to be?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ryan Christiani
Ranch Hand

Joined: May 15, 2011
Posts: 30
haha

I was expecting it to be the length of the list? I am assuming this is not right. How would i get access to the list items? Am I at least correct in thinking that a list is an array?

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61767
    
  67

Ryan Christiani wrote:I was expecting it to be the length of the list?

What list?

I am assuming this is not right.

Assumption correct. document.getElementById("images") will fetch the image element with the id of images, if any.

How would i get access to the list items?

Again, what list of items?

Am I at least correct in thinking that a list is an array?

There is no such thing as a "list" in JavaScript. There is just arrays.
Ryan Christiani
Ranch Hand

Joined: May 15, 2011
Posts: 30


aahhh sorry i should have posted the html aswell.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61767
    
  67

Ah. You'll have to get the count of <li> elements.

If you are up to using a little jQuery to make your life easier, it'd be:
Ryan Christiani
Ranch Hand

Joined: May 15, 2011
Posts: 30
Thanks for the reply.

It still seems to come up as null though. I tried what you suggested. If i put an alert in the for loop it alerts the info., actually it alerts 0,1,2,3. 4 items? The HTML has not changed, so there is only 3 items.

Also a question about my init function. I put both the colImg and detailWrite in it. colImg cause the alert was not working at first. But in reality, i would only really have to but the detailWrite in there right? because that is were all the information is processed?



Last Question. In jQuery the $ is used to replace document.getElementById as a way of selecting elements i assume.

Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61767
    
  67

Ryan Christiani wrote:In jQuery the $ is used to replace document.getElementById as a way of selecting elements i assume.

Almost, but not quite. The $() method in jQuery selects by many criteria, only one of which is by id.

For example: $('#someId') selects the element with id someId, while $('.someClass') selects all elements with class someClass, and $('a["href"$=.pdf]') selects all anchors that reference PDF documents. So you can see that it's a lot more powerful than document.getElementById().

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61767
    
  67

Are you planning on adopting jQuery? If so, this whole thing can be simplified to a handful of lines of code. Depends whether you want to go in for a penny or in for a pound (as the saying goes).
Ryan Christiani
Ranch Hand

Joined: May 15, 2011
Posts: 30
I hear that jQuery is pretty powerful, but i feel like I don't know enough of the basics of javascript, maybe the wrong type of thinking?

How would you handle this using jQuery?

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61767
    
  67

jQuery will let you simplify your JavaScript even if it's just basic. But to really use jQuery to its greatest extent you need a good basis in JavaScript. So it's ok to learn both as you go along.

Could you state what your goals are? It's somewhat, but not exactly, clear from the code that you have posted.
Ryan Christiani
Ranch Hand

Joined: May 15, 2011
Posts: 30
The final goal, is i have a site with thumbnail images, they are in a list and when you click on one the title would appear beside the image.

So if you click image 1 the image the image would appear and the title could appear beside it, click image 2...and so on. Does that make sense?

So my thinking in the code was to get the length of the list array, and then loop through it, send that to a function that gets the element onclick and then would place, from an array, the title of the image into the document.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61767
    
  67

Hmm, sounds a lot like the "Photomatic" plugin that I developed for jQuery in Action in chapter 7.

OK, give me a couple of hours and I'll try to fit in some time to code up an example.
Ryan Christiani
Ranch Hand

Joined: May 15, 2011
Posts: 30
Thank you!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: is null question