JavaScript help!

in swapFE() function I need to exchange grench phrase for the english translation. var phrase represents the phrase that was clicked in the document. The whole script has to change text (french for eng and vice-versa) on click.
I'm confused, please help.!!!
function eventSource(e) {
   var IE = document.attachEvent ? true:false;
   var DOM = document.addEventListener ? true: false;
   if (IE) return event.srcElement;
   else if (DOM) return e.currentTarget;
}
function setUpTranslation() {
var phrases = document.getElementsByTagName("p");
for (var i=0; i<phrases.length; i++){
	phrases[i].childNodes[1].innerHTML=french[i];
	
	var phrase = phrases[i].onclick=?
}
}
function swapFE(e){
 phrase = eventSource(e);
 var phrasenum = parseInt(phrases.previousSibling().innerHTML);

Open in new window

netxmAsked:
Who is Participating?
 
pellepConnect With a Mentor Commented:
A couple of things --

In your eventSource() function, what are you trying to accomplish with this line?:
phrase.onclick=phrases[i];

Furthermore, in your swapFE() function, this line:
var phrasenum= parseInt(phrases.previousSibling().innerHTML);
should probably be
var phrasenum= parseInt(phrase.previousSibling().innerHTML);

The same line would also need to be included in the swapEF() function. Just FYI - declaring a variable with the "var" keyword makes it local to the function in which it's declared.
0
 
alain34Commented:
Hello netxm,

Can you provide the full listing please?
You are only giving a subset of swapFE...
Also could you tell us where the french[] array is defined?

Regards,

alain34
0
 
netxmAuthor Commented:
I've made a little progress. I'm not sure that is going work.
this is engfr.js
function eventSource(e) {
   var IE = document.attachEvent ? true:false;
   var DOM = document.addEventListener ? true: false;
   if (IE) return event.srcElement;
   else if (DOM) return e.currentTarget;
  // var phrase = phrases[i].onclick;
  phrase.onclick=phrases[i];
}
function setUpTranslation() {
var phrases = document.getElementsByTagName("p");
for (var i=0; i<phrases.length; i++){
	phrases[i].childNodes[1].innerHTML=french[i];
	}
	
}
 
function swapFE(e){
 var phrase = eventSource(e);
 var phrasenum= parseInt(phrases.previousSibling().innerHTML);
 phrase.innerHTML=english[phrasenum-1];
 phrase.style.color=rgb(155,102,102);
}
 
function swapEF(e){
  phrase = eventSource(e);
  phrase.innerHTML=french[phrasenum-1];
  phrase.style.fontStyle=italic;
  phrase.style.fontcolor=black;
}

Open in new window

0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
netxmAuthor Commented:
this is french5.htm
<link href="styles.css" rel="stylesheet" type="text/css" />
<script src="french5.js" type="text/javascript"></script>
<script src="engfr.js" type="text/javascript"></script>
</head>
 
<body onload="setUpTranslation()">
<table><tr>
<td class="left">
   <b>French 101</b><br />
   MWF: 9:00-9:50<br />
   Rm. 402 Linton Hall
</td>
<td class="right">
   <b>Prof. Eve Granger</b><br />
   Office: 810 Linton Hall<br />
   Hours: TR: 3:00-4:30
</td>
</tr></table>
 
<h1>Week 5 Phrases:</h1>
<h2>Press down your mouse button on each phrase to translate</h2>
 
<p><span class="pnum"> 1</span><span class="phrase" onmousedown="swapFE()" onmouseup="swapEF"></span></p>
<p><span class="pnum"> 2</span><span class="phrase" onmousedown="swapFE()" onmouseup="swapEF"></span></p>
<p><span class="pnum"> 3</span><span class="phrase" onmousedown="swapFE()" onmouseup="swapEF"></span></p>
<p><span class="pnum"> 4</span><span class="phrase" onmousedown="swapFE()" onmouseup="swapEF"></span></p>
<p><span class="pnum"> 5</span><span class="phrase" onmousedown="swapFE()" onmouseup="swapEF"></span></p>
<p><span class="pnum"> 6</span><span class="phrase" onmousedown="swapFE()" onmouseup="swapEF"></span></p>
<p><span class="pnum"> 7</span><span class="phrase" onmousedown="swapFE()" onmouseup="swapEF"></span></p>
<p><span class="pnum"> 8</span><span class="phrase" onmousedown="swapFE()" onmouseup="swapEF"></span></p>
<p><span class="pnum"> 9</span><span class="phrase" onmousedown="swapFE()" onmouseup="swapEF"></span></p>
<p><span class="pnum">10</span><span class="phrase" onmousedown="swapFE()" onmouseup="swapEF"></span></p>
 
</body>
</html>

Open in new window

0
 
netxmAuthor Commented:
and this is french5.js
var english=new Array();
english[0]="This hotel isn't far from the Eiffel Tower.";
english[1]="What time does the train arrive?";
english[2]="We have been waiting for the bus for one half-hour.";
english[3]="This meal is delicious";
english[4]="What day is she going to arrive?";
english[5]="We have eleven minutes before the train leaves!";
english[6]="Living in a foreign country is a good experience.";
english[7]="Excuse me! I'm late!";
english[8]="Is this taxi free?";
english[9]="Be careful when you go down the steps.";
 
var french=new Array();
french[0]="Cet h&#244;tel n'est pas loin de la Tour Eiffel.";
french[1]="A quelle heure arrive le train?";
french[2]="Nous attendons l'autobus depuis une demi-heure.";
french[3]="Ce repas est d&#233;licieux";
french[4]="Quel jour va-t-elle arriver?";
french[5]="Nous avons onze minutes avant le d&#233;part du train!";
french[6]="Habiter dans un pays &#233;tranger est une bonne exp&#233;rience.";
french[7]="Excusez-moi! Je suis en retard!";
french[8]="Est-ce que ce taxi est libre?";
french[9]="Faites attention quand vous descendez l'escalier.";

Open in new window

0
 
netxmAuthor Commented:
Thank you, pellep!!!
how to assign the phrase that was clicked in the document to "phrase var"?
0
 
netxmAuthor Commented:
 phrase.onclick=phrases[I] supposed to represent clicked line...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.