This is very basic I’m sure to JavaScript but I am having a hard time so any help would be appreciated.
I want to call a function within a for loop using the mouseDown event occurring within an object’s second child node. The part italicized is my attempt to do this. The swapFE function is still a work in progress by the way. And one more thing is when I put the italicized part in the swapFE function everything works properly but when I put it in the for loop it doesn’t all show up. I don’t know why. I am basically trying to swap French phrases for English ones when I click the phrase with my mouse.
function setUpTranslation() {
var phrases = document.getElementsByTagName("p");
var swapFE = document.getElementsByTagName("phrase");
for (i = 0; i<phrases.length; i++) {
phrases[i].number = i;
phrases[i].childNodes[1].innerHTML = french[i];
*phrases[i].childNodes[1].onMouseDown = swapFE;*
}
}
/* see "function_swapFE(phrase,phrasenum);" below. The expression to call function swapFE
is located underneath "function swapFE(e)" because although the directions said to put the
"run swapFE" within the for loop it did not work properly that's why I put it beneath the
"function swapFE(e)".*/
function swapFE(e) {
var phrase = eventSource(e);
var phasenum = parseInt(1) = [1].innercontent.previousSibling;
phrase.node.previousSibling.onmousedown=swapFE
function_swapFE(e)(phrase,phrasenum);
}
}
If you have questions let me know.
Thanks for your help.
I can only make a couple of guesses at what might be failing with your source code. Firstly, the following code assumes that all
<p>tags have at least 2 child elements:If any
<p>tags on your page have less than 2 child elements, an error will be thrown and script execution will halt. The best solution for this would be to add a class attribute to each<p>tag that will contain the elements you’re looking for. Alternatively, you could just check for the existence of the second childnode with anifstatement. Or you could do both.Secondly, like all events,
onmousedownshould be declared in lowercase. SettingonMouseDownwill not throw an error, but instead create a custom property on the element instead of attaching an event handler.Finally, the following code:
will locally override the global function
swapFEfor that function, replacing it with a variable instead.This is how I might write your
setupTranslationfunction: