views:

254

answers:

2

Hi, 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.

+1  A: 

With this, you are creating a local variable named swapFE;

var swapFE = document.getElementsByTagName("phrase");

Then with this you are setting this var as a mouseDown

phrases[i].childNodes[1].onMouseDown = swapFE;*

That's not right... onMouseDown should be set to a function name, not a local variable of that name. So you should probably rename the local var to something else. That will at least get you closer to a solution.

mlathe
I see what you mean. What I'm actually trying to do (and maybe it wasn't too clear) is in the onMouseDown portion reference to the swapFE function that I am working on getting right. So I deleted the var swapFE and now I'm going to have it reference to the swapFE function below the for loop.
Ashley
A: 

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:

for (i = 0; i<phrases.length; i++) { 
    phrases[i].number = i; 
    phrases[i].childNodes[1].innerHTML = french[i]; 

    *phrases[i].childNodes[1].onMouseDown = swapFE;* 
} 

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 an if statement. Or you could do both.

Secondly, like all events, onmousedown should be declared in lowercase. Setting onMouseDown will not throw an error, but instead create a custom property on the element instead of attaching an event handler.

Finally, the following code:

var swapFE = document.getElementsByTagName("phrase"); 

will locally override the global function swapFE for that function, replacing it with a variable instead.

This is how I might write your setupTranslation function:

function setUpTranslation() {     
    var phrases = document.getElementsByTagName("p");
    // rename the swapFE var as outlined below
    var swapFENodes = document.getElementsByTagName("phrase");

    var cNode;  // set up an empty variable that we use inside the loop
    for (i = 0; i<phrases.length; i++) {
        /* Check for the existence of the translationPhrase class 
           in the <p> tag and the set the cNode var to childNodes[1]
           and testing for its existence at the same time */
        if (cNode.className != "translationPhrase" 
             || !(cNode = phrases[i].childNodes[1]))
             continue; // skip to the next iteration

         phrases[i].number = i;
         cNode.innerHTML = french[i];     
         cNode.onmousedown = swapFE;  // Changed onMouseDown to onmousedown
    }     
}    
Andy E
Thanks for your help Andy E. I see what you're saying about my code assuming I have at least 2 child elements but what I was trying to do was refer to the second child node of the object (that's why I put [1] and not [0]. Do you have an idea of how I could fix it?
Ashley
@Ashley: You can check for the existence of the node with an `if` statement. I've updated my answer with how I might write your function at the bottom.
Andy E