Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. Using this.id

Using this.id

Scheduled Pinned Locked Moved JavaScript
data-structurestoolshelp
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ASPnoob
    wrote on last edited by
    #1

    Hi all, I am trying to pass the value inside of "this.id" from one function to another function but it's not working. Basically I am trying to grab the id of a thumbnail image which is inside anchor tags, by using "this.id" when they are clicked. Then I would like to display data related to the clicked thumbnail which are stored in an array, using innerHTML. I am using the AddEventListener property inside the script tags to listen for the onclick event thus I do not have the onclick property on my webpage. The following are my functions inside the script tags:

    var songTitle =new Array("Title1", "Title2");
    function AddListeners()
    {
    if(window.addEventListener)
    {
    document.getElementById('A1').AddEventListener("click",Display_Info,false);
    document.getElementById('A2').AddEventListener("click",Display_Info,false);
    document.getElementById('A3').AddEventListener("click",Display_Info,false);
    }
    else if(window.attachEvent)
    {
    document.getElementById('A1').attachEvent("onclick",Display_Info);
    document.getElementById('A2').attachEvent("onclick",Display_Info);
    document.getElementById('A3').attachEvent("onclick",Display_Info);
    }
    }

      function imgClicked(x)
      {
          var idClicked = x; //get thumbnail id      
          var newID = idClicked.substring(0,1); //subtract first character of thumbnail id        
          var convertedID = ParseInt(newID,10); //convert id to integer         
          return convertedID;          
      }
    
      function Display\_Info()//passing in Parameter            
      {                 
          var A= this.id;
          var picID = imgClicked(A);
          document.getElementById('Title').innerHTML= songName\[picID\];                  
      }   
    

    The following are anchor tags inside the body of my webpage:



    Please take a look to see why my script is not working. Any help is greatly appreciated, thanks in advance.

    T D 2 Replies Last reply
    0
    • A ASPnoob

      Hi all, I am trying to pass the value inside of "this.id" from one function to another function but it's not working. Basically I am trying to grab the id of a thumbnail image which is inside anchor tags, by using "this.id" when they are clicked. Then I would like to display data related to the clicked thumbnail which are stored in an array, using innerHTML. I am using the AddEventListener property inside the script tags to listen for the onclick event thus I do not have the onclick property on my webpage. The following are my functions inside the script tags:

      var songTitle =new Array("Title1", "Title2");
      function AddListeners()
      {
      if(window.addEventListener)
      {
      document.getElementById('A1').AddEventListener("click",Display_Info,false);
      document.getElementById('A2').AddEventListener("click",Display_Info,false);
      document.getElementById('A3').AddEventListener("click",Display_Info,false);
      }
      else if(window.attachEvent)
      {
      document.getElementById('A1').attachEvent("onclick",Display_Info);
      document.getElementById('A2').attachEvent("onclick",Display_Info);
      document.getElementById('A3').attachEvent("onclick",Display_Info);
      }
      }

        function imgClicked(x)
        {
            var idClicked = x; //get thumbnail id      
            var newID = idClicked.substring(0,1); //subtract first character of thumbnail id        
            var convertedID = ParseInt(newID,10); //convert id to integer         
            return convertedID;          
        }
      
        function Display\_Info()//passing in Parameter            
        {                 
            var A= this.id;
            var picID = imgClicked(A);
            document.getElementById('Title').innerHTML= songName\[picID\];                  
        }   
      

      The following are anchor tags inside the body of my webpage:



      Please take a look to see why my script is not working. Any help is greatly appreciated, thanks in advance.

      T Offline
      T Offline
      twseitex
      wrote on last edited by
      #2

      .id is a property of html element (html DOM), not of a function this is the pointer of an instance like html element inside of html DOM. document.getElementById('A1').AddEventListener("click",Display_Info,false); --> this is the pointer of document.getElementById('A1') param of Display_Info is not exists like pointer or id-string-value

      A 1 Reply Last reply
      0
      • T twseitex

        .id is a property of html element (html DOM), not of a function this is the pointer of an instance like html element inside of html DOM. document.getElementById('A1').AddEventListener("click",Display_Info,false); --> this is the pointer of document.getElementById('A1') param of Display_Info is not exists like pointer or id-string-value

        A Offline
        A Offline
        ASPnoob
        wrote on last edited by
        #3

        Hi, thanks for replying. I understand that id is a property of DOM element but I've seen tutorials where "this.id" is used to get the id of an element that is clicked. Because I am using the AddEventListener property, I did not use the

        onclick="Dispay_Info();"

        in my webpage.

        1 Reply Last reply
        0
        • A ASPnoob

          Hi all, I am trying to pass the value inside of "this.id" from one function to another function but it's not working. Basically I am trying to grab the id of a thumbnail image which is inside anchor tags, by using "this.id" when they are clicked. Then I would like to display data related to the clicked thumbnail which are stored in an array, using innerHTML. I am using the AddEventListener property inside the script tags to listen for the onclick event thus I do not have the onclick property on my webpage. The following are my functions inside the script tags:

          var songTitle =new Array("Title1", "Title2");
          function AddListeners()
          {
          if(window.addEventListener)
          {
          document.getElementById('A1').AddEventListener("click",Display_Info,false);
          document.getElementById('A2').AddEventListener("click",Display_Info,false);
          document.getElementById('A3').AddEventListener("click",Display_Info,false);
          }
          else if(window.attachEvent)
          {
          document.getElementById('A1').attachEvent("onclick",Display_Info);
          document.getElementById('A2').attachEvent("onclick",Display_Info);
          document.getElementById('A3').attachEvent("onclick",Display_Info);
          }
          }

            function imgClicked(x)
            {
                var idClicked = x; //get thumbnail id      
                var newID = idClicked.substring(0,1); //subtract first character of thumbnail id        
                var convertedID = ParseInt(newID,10); //convert id to integer         
                return convertedID;          
            }
          
            function Display\_Info()//passing in Parameter            
            {                 
                var A= this.id;
                var picID = imgClicked(A);
                document.getElementById('Title').innerHTML= songName\[picID\];                  
            }   
          

          The following are anchor tags inside the body of my webpage:



          Please take a look to see why my script is not working. Any help is greatly appreciated, thanks in advance.

          D Offline
          D Offline
          dusty_dex
          wrote on last edited by
          #4

          FYI all your anchor tags src= have no opening double-quotes

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups