PDA

View Full Version : Problem with onchange attribute (I think)



drb
Nov 25th, 2009, 07:30 PM
Hi all,
I have the following code:


<html>
<head>
<title>Page Title</title>
<script type='text/javascript'>
mymusic=[];
tracks=[];
mymusic=[{title:"a",artist:"b",artwork:"c",tracks:[{tracktitle:"d",trackmp3:"e"},{tracktitle:"x",trackmp3:"z"}]},{title:"f",artist:"g",artwork:"h",tracks:[{tracktitle:"i",trackmp3:"j"}]}];

function nowplaying(trackindex,albumindex){
q+="<html><head></head><body><p>Song:</p>";
q+="<p>"+mymusic[albumindex].tracks[trackindex].tracktitle+"</p>";
q+="<p>from Album:</p>";
q+="<p>"+mymusic[albumindex].title+"</p>";
q+="<img src=\""+mymusic[albumindex].artwork+"\">";
with(document.getElementById('nowplays').contentDocument){
open();
write(p);
close();
}
}

function anything(i){
p="";
p+="<html><head></head><body><form><select onchange='parent.nowplaying(this.selectedIndex,i);' size='";
p+=mymusic[i].tracks.length+1+"'>";
p+="<option>Choose a song...</option>";
for(j=0;j<mymusic[i].tracks.length;j++){
p+="<option>"+mymusic[i].tracks[j].tracktitle+"</option>"};
p+="</select></form></body></html>";

with(document.getElementById('songs').contentDocument){
open();
write(p);
close();
}
}


function showinitial(){
s="";
s+="<html><head></head><body><form><select onchange='parent.anything(this.selectedIndex-1);' size='";
s+=mymusic.length+1+"'>";
s+="<option>Choose an artist...</option>";
for(i=0;i<mymusic.length;i++){
s+="<option>"+mymusic[i].title+"</option>"};


s+="</select></form></body></html>";
alert(s);
with(document.getElementById('albums').contentDocument){
open();
write(s);
close();
}
}



</script>
</head>
<body onload="showinitial();">
<style type='text/css'>
div { padding:0;margin:0; }
.container { position:relative; width:100%; height:100%; }
.topcontainer { height:65%; }
.box { float:left; width:32%; height:90%; padding:5px; border:1px solid black; overflow:scroll; }
.bottom { height:35%; border:1px solid black; }
</style>
<div class="container">
<div class="topcontainer">
<div class="box"><p>Albums:</p><iframe id='albums'>hi </iframe></div>

<div class="box"><p>Songs:</p><iframe id='songs'></iframe></div>
<div class="box"><p>Now Playing:</p><iframe id='nowplays'></iframe></div>
</div>
<div class="bottom"></div>
</div>

</body>
</html>

which, as you will see if you run it, creates an iframe-based media player based on a collection of albums, and songs within these albums (I just used letters to define these albums and songs in the mymusic array, for simplicity). Focusing on the top 3 iframes, the way I have set out the user interaction is to generate the HTML for forms of available albums and songs using Javascript, and write them to the iframes in the body. This nearly works fine: if you run it and make a selection in the 'Albums' menu, you will see that the options this causes to display in the 'Songs' menu correspond with the mymusic array, so this works. However, when I choose a song, the function "nowplaying(trackindex,albumindex)" should be called using an onchange event in the 'Songs' form, in the exact same way as in the form generated using "showinitial()"...but the function isn't called. I have ruled out the coding of "nowplaying" itself as a cause, because even when I change "nowplaying" to sth simple like alert("hello"), with no variables or anything being passed in, it still doesn't call. So this leads me to think the problem is with the onchange attribute in "anything", but I can't see what the problem is. The way I coded it is no different to before, and that worked fine, so why won't this work?

Any help would be much appreciated!