Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

is null question

 
Ryan Christiani
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Explain: document.getElementById("images").length

What are you expecting this to be?
 
Ryan Christiani
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


aahhh sorry i should have posted the html aswell.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic