murday1983
murday1983

Reputation: 4026

Call jQuery Function From Any Page

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>

Upvotes: 0

Views: 59

Answers (1)

Muhammad Bilal
Muhammad Bilal

Reputation: 84

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

Upvotes: 2

Related Questions