0

I need a click JQuery function in a global file so it can be used throughout my website so i don't have to keep repeating code.

Below is my file structure

|
|->index.html
|->index.JS
|->componentsFolder
   |
   |->page1Folder
      |->page1.html
      |->page1.js
   |->page2Folder
      |->page2.html
      |->page2.js

I tried adding the following in my index.js as this file is loaded in the index.html file and is loaded in all page but none seem to hit

$('body').delegate('button', 'click', function () {
    alert('click2')
});

$('.roundButton').on('click', function () {
    alert('click1')
})

$('button').click(function () {
    alert('click3')
})

This is the button HTML in my page2.html file

<button id="addReselNextButt" class="btn btn-info roundButton">Test</button>

This is at the very end of my index.html file and is where i load the index.js file and is loaded on every page so why doesn't it hit any of them, what am i doing wrong as the code i need to put in this will seem very repetitive across my 50pg website hence why i thought i'd be able put it globally somewhere and call it when needed

<script type="text/javascript" src="index.js"></script>
3
  • 2
    If you call that from every page then you need to make the path relative to the root of the domain: src="/index.js" Commented Aug 1, 2019 at 14:48
  • And wrap in $(function() { ... }); to execute at load time Commented Aug 1, 2019 at 14:49
  • stackoverflow.com/questions/37857036/… Could also use subscribe/unsubscribe Commented Aug 1, 2019 at 14:50

1 Answer 1

2

make a file name helper.js and put your common jquery code in it and include it where you want

make sure you include your jquery before include helper.js file

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.