SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Evangelist bradical1379's Avatar
    Join Date
    Feb 2007
    Posts
    443
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to get ID from element?

    I am trying to get the ID of this element after it has been clicked on. I am stuck on how to get the ID extracted from the element.

    Here is my HTML:

    Code:
    <div id="projectPhoto">
    					<div id="photo_200" class="photogallery">
    						<img src="/path/to/photo/200_001.jpg" width="228" height="166" id="photo_1" alt="" />
    					</div>
    				</div>
    And the JavaScript:

    Code:
    window.onload = function() {
    
    	if ($('projectPhoto')) {
    	Event.observe($('projectPhoto'), 'click', 
    	function(a){
    		var images = $$('div.photogallery');
    		alert(images);
    	}, false);
    	}
    	
    }

  2. #2
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Should just be images.id

  3. #3
    SitePoint Evangelist bradical1379's Avatar
    Join Date
    Feb 2007
    Posts
    443
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chris_fuel View Post
    Should just be images.id
    I tried that, but it returns undefined.

  4. #4
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh I see, it returns not an HTML Element, but a list of them. Give this a go:

    images[0].id

  5. #5
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What framework are you using?

  6. #6
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    in your onclick function try
    alert(this.id);

  7. #7
    SitePoint Evangelist bradical1379's Avatar
    Join Date
    Feb 2007
    Posts
    443
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SoulScratch View Post
    What framework are you using?
    prototype.js

  8. #8
    SitePoint Evangelist bradical1379's Avatar
    Join Date
    Feb 2007
    Posts
    443
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chris_fuel View Post
    Oh I see, it returns not an HTML Element, but a list of them. Give this a go:

    images[0].id
    This worked perfectly. Thanks!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •