0

I am trying to implement a javascript on my page and I want to refer to a function with the onclick property. How would I do this, how would I change my following code so that is actually runs the script correctly?

$(document).ready(function()
{
	function dosomething()
  {
  	alert('I just did something');
  }
  
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<input type="button" class="btn btn-default" value="Kortingscode" onclick="dosomething()">

Thanks in advance.

4
  • 2
    Because the function is inside of the ready so it is not global. Commented Mar 25, 2016 at 14:28
  • You should use the document.ready just when you want to run something at that moment. What you want to achieve is to create a function that can be called globally, so just declare it without that document.ready Commented Mar 25, 2016 at 14:33
  • Possible duplicate of Using jQuery to trigger html onclick event Commented Mar 25, 2016 at 14:37
  • There's no reason to use $(document).ready in this case. The code within doesn't need the document for anything. Commented Mar 25, 2016 at 14:37

4 Answers 4

4

Move the dosomething function outside the ready function on a global scope

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

3 Comments

On stackoverflow it works, but it doesn't work in my fiddle. Code works fine now.
I'm sorry, didn't understand, does it work now or not?
Yes it works fine now. Thank you. I will mark this answered in a couple minutes.
2

An other solution is adding the event to the button on the document.ready

$( document ).ready(function() {
   var button = document.getElementsByClassName("btn")[0];

   button.onclick=function(){
      alert('Hi');
   };
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" class="btn btn-default" value="Kortingscode">

Comments

0

You can easily use jquery for that.

First load the jquery script.

<script   src="https://code.jquery.com/jquery-1.12.2.min.js""></script>

Then

<input type="button" class="btn btn-default Kortingscode" value="Kortingscode">

The jquery:

   <script>
    $(".Kortingscode").click(function(){
        alert("The button was clicked.");
    });
   </script>

Comments

0

Why are you using document ready event?

You have two different solution:

ONE: declare your function as global (outside document ready)

function dosomething() {
 alert('I just did something');
}

TWO: declare your function as jquery event

$(document).ready(function() {
  $( ".btn-default" ).click(function() {
    alert('I just did something');
  })
)};

Make sure your javascript code is inside a <script></script> tag.

2 Comments

What do you mean by "declare your function as a jquery event"? I want to call the function within the onclick property, how would it look like, could you refer me to an example of some sort?
As I already said, just don't use $(document).ready( but put directly the function name (so it becomes global)

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.