Javascript help, please?
Asked by
netxm (
288)
February 17th, 2009
here I need one thing – var phrase has to represent phrasethat was clicked, onmousedown created in html
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];
}
}
function swapFE(e){
var phrase = eventSource(e);
var phrasenum=parseInt(phrase.previousSibling());
phrase=english[phrasenum-1];
phrase[i].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;
}
Observing members:
0
Composing members:
0
18 Answers
What’s not working?
One thing that I see is that “phrasenum” is defined in the swapFE function and you’re trying to use it in the swapEF function.
Perhaps waiting more than ten minutes before you beg for more help might be good idea…
I will define it again, what’s not working is It doesn’t recognize click I probably have to assign something to var phrases , so it will recognize what line was clicked ; here is what I have in HTML (onmousedown=“swapFE()” onmouseup=“swapEF”)
what exactly is in the html ?
<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>
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ô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élicieux”;
french[4]=“Quel jour va-t-elle arriver?”;
french[5]=“Nous avons onze minutes avant le départ du train!”;
french[6]=“Habiter dans un pays étranger est une bonne expé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.”;
It seems to me that you’re trying to do too much at once. When you make this site, first make something that works with the swapFE() and swapEF() code, with text in the spans. Then work on populating the spans.
Does “setUpTranslation” actually put text in the spans? Right now there’s nothing in the spans with the mouse events for you to click on, so unless those spans get populated, there’s nothing to click on.
<span class=“phrase” onmousedown=“swapFE()” onmouseup=“swapEF”> it’s mouse event, isn’t it? Any suggestions ??? Thanks for replies
Right, that is a mouse event, but it’s just an empty span
<span…. > </span> with nothing between those two tags creates nothing on the page.
if you had something like
<span….> text here </span> then if you clicked on the words “text here” your mouse events would be triggered.
setUpTranslation() replaces second child node of <p> for item from array
function setUpTranslation() {
var phrases = document.getElementsByTagName(“p”);
for (var i=0; i<phrases.length; i++){
phrases[i].childNodes[1].innerHTML=french[i];
}
Right. Does that actually work? When you look at the page, do the phrases show up?
yes, but if I click on them nothing happens, I’ve tried with alert(), within swapFE() – it works, but I need that on click it pulls out data from array, replaces french for english.
Perhaps
var phrasenum=parseInt(phrase.previousSibling());
should be
var phrasenum=parseInt(phrase.previousSibling().innerHTML);
since previousSibling() refers to the object, not the contents?
looks right! and how do I assign phrases (click) to array[i]?
to what array?
I think the swapFE function needs some work:
function swapFE(e){
var phrase = eventSource(e);
var phrasenum=parseInt(phrase.previousSibling().innerHTML);
phrase.innerHTML=english[phrasenum-1];
phrase.style.color=rgb(155,102,102); /*Removed [i] */
}
I think for me to fully debug this I’d just have to have all of the files involved, I’ll send you a private message…
Answer this question
This question is in the General Section. Responses must be helpful and on-topic.