• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

getting information from a clicked image

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not a programmer, but I'm trying to create a simple advent calendar for some students.

The idea is I have a tree (background image) and on the tree I place ornaments. Each ornament is a separate png graphic that is clickable.

How do I know which image is being clicked? I searched around and I found the get attributes attr() feature but I seem to be using it incorrectly as it only seems to grab the info from the first graphic loaded.


in HTML file
       

in SCRIPT FILE



So, this will bring up an alert with '3' no matter which one I click. Eventually, I want to use that information to display the proper question ... if I get that far!

Any help to point me in the right direction is greatly appreciated!
 
Sheriff
Posts: 28401
100
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jonathan, welcome to the Ranch!

I've added your post to the forum where JavaScript questions are usually found... if my JS skills were up to date I'd give it a try myself, but there's plenty of other people here with JS skills.
 
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The jQuery selector $('img') matches any element of type img, so you're getting a matched set of all images. Since attr() only operates on a single element, it's giving you the value of the first image. Always.

Your post contains little context. Is getInfo() called from a click handler? If so, inside a click hander, the clicked element is found via this.

So, something like: would be more likely what you are looking for. But you need to be careful, the value of this is per function, so why do you have a separate getInfo() function in the first place? Why not just do this work in the click handler?

P.S. This is a misuse of alt which has its own distinct purpose. If you are going to use an attribute for your own purposes, I suggest you use a custom attribute. In HTML5, custom attributes start with "data-", so you can create any that you like, data-fred, for example, where "fred" can be anything that you want (and makes sense for what you are using it for).

P.P.S. If you are just looking for a distinct id for each image, you already have that in the id attribute. Why add another?
 
Jonathan Hamelin
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the replies! I tried to find the right place to post my question, but I guess I didn't get it right.

I really appreciate the responses! Just to make sure you understand WHY I did some of these things, I really want to be clear. I am NOT a programmer! I did a short course online with khan academy to get started and I've been messing around a bit in my spare time but not consistently, so I'm always forgetting what I've learned.

I thought it would be nice to put an easy advent calendar online for some of my young students. I first made a ton of separate pages realizing it was a mistake (but it was easy because I could just use hyperinks). I then thought it would be better to put the data in an array and use just one page, but that would require some programming. *sigh*.

It took me a long time to just figure out how to pass the information with the getInfo command, but obviously, I don't know what I'm doing. Yes, I hijacked the alt tag, but to be fair, they were just dates of the images anyway, and I used numbers instead of ordinals.... ie) 3 instead of 3rd. So, really...it was kind of the same! I eventually found this command: var imageName = (event.target.alt); which I used and it seems to work. Maybe it's the same thing you suggested?

Anyway, my code file is fairly short and simple so hopefully it won't break. It's just a simple advent calendar anyway! You can see what I did here: http://www.greenwood-ac.com/christmas/Christmas.html

I'll be taking it down in the new year, so it won't be there in 2019.


If you really want to see the code, I'll include all the code and webpage, but I warn you it's not 'pretty'. But...it's working!


 
Jonathan Hamelin
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my code. I couldn't send it as an attachment.

 
Jonathan Hamelin
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Finally, my CSS file
 
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I checked your advent callendar. When I click one of your ornaments, nothing happens.
When I open the browser console this is the error:


What are you trying to do actually?
If you need to know which image is being clicked why not just pass the value to your method?
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could try something like this (I only maded three objects cause I was to lazy to copy paste everything but you get the gist ):



In your html's body tag put onload(createArray) and when calling the getInfo method pas the index of the array so 3rd december would be getInfo(0), 4th december getInfo(1) and so on...
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lol, I only see now that this post is over a week old, so most ikely you won't need it anymore.
 
Jonathan Hamelin
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Daniel, thanks for the information. I wanted to pass the value onto the function but I really don’t know what I’m doing. I was also trying to do an array like you suggested but I couldn’t figure out the proper syntax and I didn’t want to retype the heading parts over and over again.

Anyway, I suppose I should have said that I got it to work for Chrome and Explorer but when I tried Firefox it wouldn’t do anything but I don’t know why.

What happens in Chrome is that the question pops up for the appropriate day. If you click on answer, it will show up only if it’s the same day or
Earlier but not for future dates.

I’ll see if I can do it your way and change it for next year or if I plan to make something else that’s similar.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic