0

I'm still new to jquery and all that, and right now I'm having a little problem that might just border on the lack of basic knowledge. So I just wanna say sry and thanks in advance for reading:

I have 2 html files ("main.html" and "content.html"). Both uses some jquery, which i have within one .js file ("script.js"). The jquery are some basic clicks on div-elements etc.

Now, the "main.html" is my base, so having header info, js-links, css-links, body etc. The "content.html" file has just some divs. Now I load the "content.html" within the "main.html" file via:

$.get('content.html', function(data) { $('body').append(data);});

well that works perfect, so far so good. Problem is, that the click functions that refer to divs within the "content.html" just don't work now. They only work when I declare/load the "script.js" within the "content.html" aswell. But now the js-file is loaded twice, triggering click functions for divs in the "main.html" file twice.

How can I fix that? Can I just reinitialize the js-file instead of loading it a new or something?

Thanks again, ANB_Seth

ANB_Seth
  • 91
  • 1
  • 9
  • can you add click event code here ? – Nishit Maheta Mar 18 '15 at 09:49
  • Okay I've read throu the article linked to, yet I can't seem to get my head wrapped around it. When I have a code like: `$('#btn').on("tap",function(e){ e.preventDefault(); //do something here! });` yet the "btn" div is in the loaded html file, and the js.file containing this code-snippet is already loaded, how can I make the js.file find the div in the appended html? Sorry for asking again :-/ – ANB_Seth Mar 18 '15 at 10:30
  • use this code . $(document).on("tap",'#btn',function(e){ e.preventDefault(); //do something here! }); – Nishit Maheta Mar 18 '15 at 10:32
  • okay thank you!!! ... I could for whatever reason just NOT see that -___-. Now I do and it works like a charme. Thanks for helping out :-) – ANB_Seth Mar 18 '15 at 10:42
  • happy to help you :) – Nishit Maheta Mar 18 '15 at 10:46
  • okay just oooooooooone more understanding follow up ^^: when I dont want to code an event on, but refer to a specific div in an attached html file e.g:... #headline ... how would I code that? atm I just use `$('#headline').toggleClass('active');` ... how would I need to rewrite that? – ANB_Seth Mar 18 '15 at 10:57

0 Answers0