Using this.id
-
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.
-
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.
.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
-
.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
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.
-
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.