Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
20
rated 0 times [  27] [ 7]  / answers: 1 / hits: 19012  / 11 Years ago, fri, november 22, 2013, 12:00:00

I should start off by saying that if there is a question like this I'm very sorry but I couldn't quite find a definite answer.



I'm quite new to JavaScript but I was wondering how in a function using if statements you could find out if a specific image had been clicked (Image ID's as 1,2 and 3) and then display appropriate text below it that appends to a previous paragraph.



The var's in this (text1 etc.) just have the text in quotations so they're fine, so I was wondering what I could do to change this code so it doesn't keep printing text1 no matter what kind of image I click.



(Although I think this is due to it being read that if an ID is there it just prints it, so that's why text1 is always printed)



Code snipped.


More From » html

 Answers
9

The best structure solution is:



HTML



<a href=javascript: changeText(1);>
<img src=abc.jpg alt=abc />
</a>
<a href=javascript: changeText(2);>
<img src=abc.jpg alt=abc />
</a>
<a href=javascript: changeText(3);>
<img src=abc.jpg alt=abc />
</a>
<div id=div></div>


Javascript



function changeText(value) {
var div = document.getElementById(div);
var text = ;

if (value == 1) text += this is one;
if (value == 2) text += this is two;
if (value == 3) text += this is tree;

div.innerHTML = text;
}

[#74114] Thursday, November 21, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
saulthomasb

Total Points: 326
Total Questions: 98
Total Answers: 93

Location: Jordan
Member since Sun, Dec 26, 2021
2 Years ago
;