24

I have created my problem on JSFiddle at https://jsfiddle.net/kgw0x2ng/5/. The code is as follows

HTML CODE

<div class="loading">Loading&#8230;</div> 
<button type="submit" onClick="hideButton()">Hide</button>
<button type="submit" onClick="showButton()">Show</button>

JS CODE

function hideButton(){
   $(".loading").hide();
}
function showButton(){
   $(".loading").show();
}

I am showing a spinner and I would like the spinner to hide when I click on the "Hide" button. I am getting the following error :

Uncaught ReferenceError: showButton is not defined
    at HTMLButtonElement.onclick (VM282:180)
onclick @ VM282:180
VM282:179  

Uncaught ReferenceError: hideButton is not defined
    at HTMLButtonElement.onclick (VM282:179)
onclick @ VM282:179.

Can someone please suggest a solution?

Thanks

Sachin

Nurjan
  • 5,889
  • 5
  • 34
  • 54
sachinb
  • 425
  • 2
  • 6
  • 10
  • 6
    Your function should be outside `document.ready` function. – Sandeep Kushwah May 30 '18 at 19:21
  • Your issue is not javascript related. It is of Jsfiddle. As you use $(...) you are using JQuery, but you never referenced Jquery in your code. In the top left of your javascript code editor there is a dropdown, select into FRAMEWORKS & EXTENSIONS the JQUERY, by instance JQuery 3.4.1. Then your code works as your expected. – elpezganzo Dec 05 '20 at 02:28

2 Answers2

3

Place your script inside the body tag

<body>
  // Rest of html
  <script>
  function hideButton() {
    $(".loading").hide();
  }
function showButton() {
  $(".loading").show();
}
</script> 
< /body>

If you check this JSFIDDLE and click on javascript, you will see the load Type body is selected

Ambo100
  • 1,185
  • 3
  • 15
  • 28
brk
  • 48,835
  • 10
  • 56
  • 78
-3

Same Problem I had... I was writing all the script in a seperate file and was adding it through tag into the end of the HTML file after body tag. After moving the the tag inside the body tag it works fine. before :

</body>
<script>require('../script/viewLog.js')</script>

after :

<script>require('../script/viewLog.js')</script>
</body>
Arindam
  • 675
  • 8
  • 15