Adding jquery to the index.php

Adding jquery to the index.php

Hello guys'n'girls,

i'm currently working on a gallery for myself and learning a lot about jquery. Today i got stuck at one point and i hope you guys can help me out.

The thing is that i wanted to automatically create a mousebutton listener for a gallery overview i have. Let me explain this with a little more depth. I'm having a script which scans a folder for subfolders and copys those subfolder names into an array. Those subfolder are my different themed galleries. In every subfolder are pictures that will also be written in an array. Thats because i don't want a lot of work adding folder/galleries and pictures.

When the user will click on the galleries overview button jquery will add an <script></script> tag with the automatically generate button listener to the index.php. The problem is that the new generate listener does simply not work. If i copy the code that is generated in implemented automatically into the index.php and write it static into the index.php the code itself works fine. 

Is there a problem with code that is added to the index file afterwords?

Here my code jqeury code:
  1. for(var i = 0; i < ordner.length; i++)
  2. {
  3. console.log("Schleife wird duchlaufen");
  4. var ordnerDivName = ordner[i].substring(2,7);
  5. var neueGallerie = ordner[i].substring(2);
  6. //console.log(neueGallerie);
  7. $('script').append("$(document).on('mousedown', '#"+ordnerDivName+"', function(event) {console.log('Der Button wurde Gedrückt!');if(webseiteMomentan!='Gallerie'){$('#foto').fadeOut('slow', function() {$('#foto').empty();$('#webseite').empty();seiteGallerieEinfuegen('"+neueGallerie+"');});}});");
  8. }
And here is the generated one (just the first button)
  1. $(document).on('mousedown', '#Aperi', function(event) {console.log('Der Button wurde Gedrückt!');if(webseiteMomentan!='Gallerie'){$('#foto').fadeOut('slow', function() {$('#foto').empty();$('#webseite').empty();seiteGallerieEinfuegen('Aperitif');});}});
Hope you guys understand my problem and have a solution for me.

Thanks!
Marcel

The website i'm writing about;  http://marcelexner.de/neu/ (veryveryalpha)