0

Javascript: need help with code!

I'm writing code for a gallery in which every time you mouseover a picture the text of a caption changes with the name of the place shown in the image. My first idea for this was to use the value of the Id (which is a number, for that purpose) of the container of the image to act as a value within an array, where the corresponding caption is stored. I don't know if this is possible, i've been trying it but mainly i'm not sure how to capture the id of an object in a variable without having to reference it in the code itself. Maybe i'm thinking this in the wrong way. All suggestions and directions are appreciated. i have no more code written than this, which at the moment doesn't work. var castle ["Castillo de Cahir- Condado de Tipperary- 1142", ""Castillo de Cahir- Condado de Tipperary- 1142"", ""Castillo de Cahir- Condado de Tipperary- 1142"", "Castillo de Ormonde - Condado de Tipperary - 1600", "Castillo de Ashford - Condado de Mayo - 1400", "Castillo de Ashford - Condado de Mayo - 1400", "Castillo de Dublín - Ciudad de Dublín - 1700", "Castillo de Dublín - Ciudad de Dublín - 1700", "Castillo de Doonagore - Condado de Claire -1500", "Castillo de Doonagore - Condado de Claire -1500", "Castillo Manderley - Ciudad de Dublin -1840" ] /*this function gets triggered from an mouseover event in a container div for the image*/ function idrecolector(){ var nro= this.id.value infotext() } /*and this function is supposed to replace the text of the caption with the text stored in the array corresponding to the id number of the object*/ function infotext() { documentGetById("subtexto").innerhtml = castle[nro] } Many thanks in advance

23rd Aug 2017, 4:59 PM
Milo Il Giovane
Milo Il Giovane - avatar
2 Respuestas
+ 8
Probably you want to make something like this: https://code.sololearn.com/WJD9fWckogeR/?ref=app
23rd Aug 2017, 5:29 PM
Maz
Maz - avatar
+ 2
This is just an idea, create a variable, let's say, currentImg, then make the code that updates the img to also update value of currentImg so you can figure which picture is currently on show, and select the appropriate string to display on the infoText function.
23rd Aug 2017, 6:00 PM
Ipang