0

I've been trying to implement a JS call from a seperate script and despite referencing m,ultiple demo I cannot get my example to work.

Can someone help me trouble shoot this simple example?

tescript.php says

<!DOCTYPE HTML>
<!-- Load checkout JS -->
<script type="text/javascript" src="tscript.js"></script>

<div class="col-md-3 col-sm-4 col-xs-12 imageUploadSide">
    <ul>
    <li> <button id="buyingPhoto" class="btn btn-buy">A New Buyit Button</button>   
   </li>
   </ul>
 </div>

Then the script file says

// checking script load & log
console.log("script file has been loaded")

$('#buyingPhoto').on('click', function() {
console.log("Can a button be depressed?")
});

I get a console log that the script has loaded (troubleshooting step 1) but the onclick itself doesn't run

4
  • Did you verify that the JavaScript file's name was named the same as the name you specified in your HTML code? Commented May 17, 2020 at 7:08
  • Try placing the script before </body> at the end of your page or at least after your HTML. Commented May 17, 2020 at 7:08
  • 1
    Did you import jquery library ? Commented May 17, 2020 at 7:09
  • The tscript.js reference is proved as I receive the "script file has been loaded" console log Commented May 17, 2020 at 8:57

2 Answers 2

4

You are missing to import jQuery library .$() is jQuery using. And be careful about order of libraries. And be sure also page is loaded. So you should use document.ready.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="tscript.js"></script>

console.log("script file has been loaded")

$( document ).ready(function() {
    $('#buyingPhoto').on('click', function() {
   			 console.log("Can a button be depressed?")
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-md-3 col-sm-4 col-xs-12 imageUploadSide">
    <ul>
    <li> <button id="buyingPhoto" class="btn btn-buy">A New Buyit Button</button>   
   </li>
   </ul>
 </div>

without jquery

document.getElementById("buyingPhoto").onclick = function() {
console.log("Can a button be depressed?")

};
<div class="col-md-3 col-sm-4 col-xs-12 imageUploadSide">
    <ul>
    <li> <button id="buyingPhoto" class="btn btn-buy">A New Buyit Button</button>   
   </li>
   </ul>
 </div>

or you can give click in html

function test(){

console.log("Can a button be depressed?")

}
<div class="col-md-3 col-sm-4 col-xs-12 imageUploadSide">
    <ul>
    <li> <button onclick="test()" id="buyingPhoto" class="btn btn-buy">A New Buyit Button</button>   
   </li>
   </ul>
 </div>

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

7 Comments

Thank you., I have added the jquery as you have demonstrated (i knew it'd be something simple) However it still does not work on my server. Is there something else I should check?
Did you put it above of another script tag? Order of script tag is important
This is the entire .php file 'code' <!-- Load checkout JS --> <script type="text/javascript" src="tscript.js"></script> <!-- Load JQuery Library --> <script src="cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/…> <div class="col-md-3 col-sm-4 col-xs-12 imageUploadSide"> <ul> <li> <button id="buyingPhoto" class="btn btn-buy">A New Buyit Button</button> </li> </ul> </div>'
ok put jquey script above the another script. Check my answer again and look at their orders
thanks for helping. I have changed the order to your response (in fact i copied it exactly) for both files and it doesnt work. I only have a cookie warning in the console
|
1

Your whole code is correct. You have not included jquery library, just include the below CDN in your <head></head>section.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

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.