I have searched other answers for an resolution to this, but it seemed everyone was simply placing their external .js file before the google api jquery link. This, however, is not my case.
html file (named "index.html"):
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="style.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="script.js"></script>
</head>
I have this in the body of my html file to create the button:
<div class="input">
<input type="button" id="test" value="CLICK ME" style="width: 500px; height: 100px" />
</div>
script file (named "script.js"):
$(document).ready(function(){
$("#test").click(function(){
$(this).hide();
alert("You clicked it!");
})
})
When I click the button on my site, nothing happens. I have tried multiple configurations of the order for the script file and google api, but nothing else has worked for me.
However, I do know that my script works because when I place my code inside the html file, under the script tag, my code works (the button vanishes and displays the alert). I'm assuming there's a syntax error of some sort in my file linking, but I'm not sure.
$(this.hide());? You mean$(this).hide();srcpath. Check the network console in Chrome and see if thescript.jsfile is loading correctly (as opposed to not found). Also, get any console errors? (Note that I am taking your word that it works correctly directly in HTML, obviously what Suresh says is correct, but perhaps that's a typo as you claim it works fine)thisis the DOM element, it has nohide()method. You need the jQuery object, so you need$(this)instead ofthis