0

I try make button with jQuery I call the JavaScript function, but I got problem :

  1. after page loaded, first time click on mybutton there is no reaction
  2. second click will execute function twice
  3. third click will execute function three and more

Why my code execute many more ? I just want " 1 click 1 execution JavaScript function" my code like this

<script type="text/javascript" language="javascript">

function showcase(){
    var foo = function () {
         alert('tutup');
    };
    $('#create-new').on('click',foo);
    return false;
}</script> <button class="button" id="create-new"onclick="return showcase();">show text</button>

please help me out this problem

for try my full error code at here http://jsbin.com/ovucer/1/edit

2
  • thx u all.. i get the answer... very helpfull..thx u. CASE CLOSED Commented Jul 24, 2013 at 1:48
  • 2
    show us the right answer ^^ Commented Jul 24, 2013 at 8:55

5 Answers 5

3

You are registering multiple click event listeners to the element

Every time you click on the button you are adding a new click handler to the button using showcase method, it is not needed

<button class="button" id="create-new">show text</button>
<script type="text/javascript" language="javascript">
    var foo = function () {
         alert('tutup');
    };
    $('#create-new').on('click',foo);
</script>
Sign up to request clarification or add additional context in comments.

Comments

2

This happens because you bind a click event, which calls showcase() function, which as well binds a new click event, that calls foo() method. And the last iteration is repeated every time you click the button. This is sort of recursion working here.

The right way will be to bind a click event a single time, after the element is loaded:

<button class="button" id="create-new">show text</button>

<script type="text/javascript">
    $("#create-new").on("click", function() {
        alert("tutup");
    });
</script>

Comments

1

You are registering an onclick function in onclick,

use this:

$('#create-new').on('click', function(){ alert('text button'); return false; });

Comments

1

You're binding the click event twice. Using onclick on the HTML and over again, using jQuery .on().

To make your life easier, and as you're using jQuery already, do it just at the document ready event:

var foo = function () {
  alert('text button');

  return false;
};

$(function () {
  $('#create-new').on('click',foo);
});

And fix your HTML bit, by removing the onclick:

<button class="button" id="create-new">show text</button>

Comments

0

try this in your script tag and delete the onclick event in the button:

var foo = function () {
    alert('tutup');
};
$('#create-new').on('click',foo);
return false;

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.