943,576 Members | Top Members by Rank

Ad:
Sep 12th, 2009
0

Help JS Event onclick to get ID

Expand Post »
im busy with a school project to create a photo viewer type site with java script.

the whole thing i being created from java script and im giving all the img's an ID tag

how do i go about getting the id of the img that i have just clicked.

i know my current code doesn't work properly in IE so help with that would be appreciated as well.

the reason i want the Id is so later i can hopefully use the info to determine which img to "zoom in" etc
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function vulpagina()
  2. {
  3. alert("vulpagina geactiveerd, jej");
  4. var maindiv=document.getElementById("maindiv");
  5.  
  6. for(i=1; i<26; i++)
  7. {
  8. var imgnr = i-1
  9. var div=document.createElement("div");
  10. var img=document.createElement("img");
  11. var br=document.createElement("br");
  12. var txt=document.createTextNode("dit is div " + i);
  13.  
  14. img.src=album[imgnr].url;
  15. img.style.margin ="10px 50p";
  16. img.style.padding ="10px";
  17. img.style.height="80%";
  18.  
  19. img.setAttribute('onclick','getImgId(this)');
  20. img.onclick = getImgId;
  21.  
  22. div.setAttribute("id","divje" + i);
  23. div.appendChild(img)
  24. div.setAttribute('style','float:left');
  25. div.style.width="20%";
  26. div.style.height="20%";
  27. div.appendChild(txt);
  28.  
  29. maindiv.appendChild(div);
  30. //alert("test");
  31. }
  32. }
  33.  
  34. function getImgId(obj)
  35. {
  36. alert('test')
  37. imgId = event.srcElement.id
  38. alert(imgId)
  39. }
  40.  
  41.  
  42. var album =
  43. [
  44. {
  45. url: 'images/0006787.jpg',
  46. landscape: false,
  47. description: 'Shadows to the sea...',
  48. photographer: 'Sander',
  49. date: new Date('12-8-1999')
  50. },
  51. {
  52. url: 'images/0004713.jpg',
  53. landscape: false
  54. },
  55. {
  56. url: 'images/0004720.jpg',
  57. landscape: false
  58. },
  59. {
  60. url: 'images/0004731.jpg',
  61. landscape: true
  62. }
  63. ];
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sereal_Killer is offline Offline
17 posts
since Nov 2008
Sep 12th, 2009
0

Re: Help JS Event onclick to get ID

hmm is there an edit button?

anyway

i changed my code a bit to using event listeners but i still cant get the Id to appear in the alert i get either HTMLElement or something like that in the alert or i get an empty alert box
anyone know what im doing wrong?.

sorry for double post but i couldnt edit my first one.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function vulpagina()
  2. {
  3. alert("vulpagina geactiveerd, jej");
  4. var maindiv=document.getElementById("maindiv");
  5.  
  6. for(i=1; i<26; i++)
  7. {
  8. var imgnr = i-1
  9. var div=document.createElement("div");
  10. var img=document.createElement("img");
  11. var br=document.createElement("br");
  12. var txt=document.createTextNode("dit is div " + i);
  13.  
  14. img.src=album[imgnr].url;
  15. img.style.margin ="10px 50p";
  16. img.style.padding ="10px";
  17. img.style.height="80%";
  18.  
  19. img.addEventListener('click',getImgId, false);
  20.  
  21.  
  22. div.setAttribute("id","divje" + i);
  23. div.appendChild(img)
  24. div.setAttribute('style','float:left');
  25. div.style.width="20%";
  26. div.style.height="20%";
  27. div.appendChild(txt);
  28.  
  29. maindiv.appendChild(div);
  30. //alert("test");
  31. }
  32. }
  33.  
  34. function getImgId(e)
  35. {
  36. var target = e.target;
  37. if (window.event) e = window.event;
  38. var srcElement = e.srcElement? e.srcElement : e.target;
  39. alert(srcElement.id)
  40. //imgId = event.srcElement.id
  41. //alert(imgId)
  42. }
Last edited by Sereal_Killer; Sep 12th, 2009 at 7:18 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sereal_Killer is offline Offline
17 posts
since Nov 2008
Sep 12th, 2009
1

Re: Help JS Event onclick to get ID

hmm is there an edit button?

anyway

i changed my code a bit to using event listeners but i still cant get the Id to appear in the alert i get either HTMLElement or something like that in the alert or i get an empty alert box
anyone know what im doing wrong?.

sorry for double post but i couldnt edit my first one.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function vulpagina() {
  2. var maindiv=document.getElementById("maindiv");
  3.  
  4. for(i=0; i<26; i++){
  5. var imgnr = i;
  6. var div=document.createElement("div");
  7. var img=document.createElement("img");
  8. var br=document.createElement("br");
  9. var txt=document.createTextNode("dit is div " + i);
  10.  
  11. img.src=album[imgnr].url;
  12. img.style.margin ="10px";
  13. img.style.padding ="10px";
  14. img.style.height="80%";
  15. img.onclick=getImgId;
  16. div.setAttribute("id","divje" + i);
  17. div.appendChild(img)
  18. div.setAttribute('style','float:left');
  19. div.style.width="20%";
  20. div.style.height="20%";
  21. div.appendChild(br);
  22. div.appendChild(txt);
  23. maindiv.appendChild(div);
  24. }
  25. }
  26.  
  27. function getImgId(){ id = this.id; alert( id ) }
Reputation Points: 120
Solved Threads: 61
Posting Pro
Troy III is offline Offline
505 posts
since Jun 2008
Sep 13th, 2009
0

Re: Help JS Event onclick to get ID

Hey junkhead, did you finish school?!!
What grade did you get?
Reputation Points: 120
Solved Threads: 61
Posting Pro
Troy III is offline Offline
505 posts
since Jun 2008
Sep 14th, 2009
0

Re: Help JS Event onclick to get ID

no grade yet. its due friday.
this is a chalange for me with onyl 2 weeks of js lessons lol.
anyway thx for the help.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sereal_Killer is offline Offline
17 posts
since Nov 2008
Sep 14th, 2009
0

Re: Help JS Event onclick to get ID

no grade yet. its due friday.
this is a chalange for me with onyl 2 weeks of js lessons lol.
anyway thx for the help.
Good luck than!
But you should have told us that the solution:
.
.
.
img.onclick=getImgId;
.
.
.
function getImgId(){ id = this.id; alert( id ) }
.
.
.
works!
Reputation Points: 120
Solved Threads: 61
Posting Pro
Troy III is offline Offline
505 posts
since Jun 2008
Sep 14th, 2009
0

Re: Help JS Event onclick to get ID

it works heh sry about that.
im not actualy using the whole function as it is but i am using it as a stepping stone. ill mark this post solved but i think im gona need more help soon with a few other functions,
thanks again for the help
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sereal_Killer is offline Offline
17 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: find text
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: run command when paste





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC