Look at the comment in there to see what I need to do. I assume I would use .childNodes to get the end result, but I’ve never worked with nodes before, how would I reference the 2nd child (aka .phrase) and loop out the array in there?
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++) {
//Here I need to reference the second child and change the inner content to french[i]
}
}
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++) {
// get ihe current P and than get all the span's,
//select the second one (id=1) and do innerHTML
phrases[i].getElementsByTagName("span")[1].innerHTML=english[i]
}
}
Thanks, that works. How would I, upon click, swap out the text for the other language? I don’t know how to add onClicks (using unobtrusive JS). I assume the condition ls to check what array is being spat out, and the action would be to swap out the language (using if/else?)
phrases[i].getElementsByTagName(“span”)[1].innerHTML=english[i]
I can’t word things properly over the internet, blah. Hopefully you all understand. Thanks so far.
Assuming you have the French text in an array, you could do this:
function setUpTranslation() {
var phrases = document.getElementsByTagName("p");
for (var i=0, j = phrases.length; i < j; i++) {
phrases[i].onclick = function() {
this.lastChild.firstChild.nodeValue = french[i];
}
}
}
var french = [
'un',
'deux',
...
];
Pretending that attachEvent will tell you if IE is being used is NOT correct. Opera supports it too. You should do object detection for whatever it is you directly want to use:
function eventSource(e) {
return window.event ? window.event.srcElement : e.currentTarget;
}