views:

155

answers:

2

Hi,

I have a button named okbutton.

A callback function is bind to the click event of the okbutton during page load

$('#okbuttonid').bind('click',function() { alert('here'); })

The first time i click the okbutton it alerts once. as expected.

When i click the next time it alerts twice and for the third time it alert thrice and it goes on. same is for $('#okbuttonid').click = function() { alert('here'); }

But

when i do it like

document.getElementById('okbuttonid').onclick = function() { alert('here'); }

it alerts only once. for every click it alerts only once.

when is with the bind event.

it it like that the alert is called the number of times we bind its event.

why is it so?

do we have to remove every time we bind.

Shall i use the native event style instead of bind?

The native event bind is working well but i want to know what this jquery bind does


I am editing/adding the following after the first answer by Nick Craver


  1. I have a list which needs a change status option(totally 4 options).
  2. I display a change status link which when clicked displays a status box and on ok button which is positioned absolutely above the change status link.
  3. i display it like a tooltip popup.
  4. at the time of showing the statuschange popup i am attaching the okbutton onclick to a function which when clicked sends an ajax request.
  5. above is my scenario.

for this i dont want to use document.ready

my code is like the following

<div id='statusdiv' style='display:none; position:absolute;'><!-- and more styles -->
<select><!-- list of options --></select>
<input id='okbutton' value='OK'>
</div>

when the user clicks changestatus link i show this div and bind a callback for the click event of okbutton.

this html part could be totally dynamic so no inline event attach.

function changestatus()
{
   //display the statusdiv absolutely above the mouse event triggered place
   $('#okbutton').bind('click',function() {
     //ajax request
   })
}

The native event bind is working well but i want to know what this jquery bind does

+1  A: 

You're attaching multiple event handlers, one each .bind() call, just don't call this multiple times:

$('#okbuttonid').bind('click',function() { alert('here'); })

an you won't get multiple alerts...just call it once on document.ready, for example:

$(function() {
  $('#okbuttonid').bind('click',function() { alert('here'); })
});
Nick Craver
Hi, i have expanded my question with additional real example. Please see the section after the heading "I am editing/adding the following after the first answer by Nick Craver"
Jayapal Chandran
@Jayapal - I don't understand your aversion to `document.ready`...this is exactly what it's used for. The native works because you *replacing* the `onclick`, which is still extra unneeded work, why not just bind once and not do that extra work?
Nick Craver
because, as i have said the div and the ok button will be totally dynamic. in this case how do i include in document.ready?
Jayapal Chandran
@Jayapal - You can use `.live()` to bind it once on ready and do nothing when it loads, for example: `$(function() { $('#okbuttonid').live('click',function() { alert('here'); }); });`
Nick Craver
oh. i used live inside the changestatus function. but you have said it to use during document ready. but still i was not able to solve my question... what about removing the bind?
Jayapal Chandran
@Jayapal - If you used it inside of `document.ready` you wouldn't have to remove the bind...it would only bind once, there should be *nothing* related to this inside your `changestatus` function, that's the point...
Nick Craver
Yes. I totally understand your point. But let us forget about giving inside document.ready... i am making it sure that i want to do some thing like that in side my function like i have did in the code. then what should i do... besides i want to know why it calls multiple times?
Jayapal Chandran
@Jayapal - It doesn't call it multiple times, it calls it **once** for *each* time you bind it...and you're doing that every time you call the function, which is incorrect and inefficient. You're not binding correctly, I'm telling you how to do it correctly and you're ignoring that advice, so I'm really not sure how to help you.
Nick Craver
don't get me wrong. I said i understood. it is this answer i want. your last comment. Now i agree with you. You said it is inefficient. Here i would like to ask you this. How about this document.onclick = function() { alert('here'); }. How does this work. is it getting over written. Hope this too is inefficient. (or) do i have to ask this as another quesiton?
Jayapal Chandran
and another thing is. i wrote this code so it would work like a jquery plugin but i have not yet completed it fully. so when i will have a funciton to call just showstatus(something) and the dialog appears then when i click ok button every thing should take care automatically like the sexy alert box plugin... during that time how will i use document.ready because i will not even know what the id i have given to the ok button. i want the total code to be inside one funciton like i have given. except the html. [cont in 2]
Jayapal Chandran
still i said the ok button is created dynamically and will not even give an id. during that time which will be i may use the bind or the native bind. it is working as expected and i had a doubt and asked this question. and you have given the answer. fine. but . still what is with this bind and document.onclick = ... ?
Jayapal Chandran
what about this answer by $(function() { $('#okbuttonid').unbind('click').bind('click',function() { alert('here'); })}); by http://stackoverflow.com/users/454979/revaxarts who has replied after you.
Jayapal Chandran
besides... I really thank you for you patience to answer me. I have given a score. cause this is how i can thank you. and finally we can conclude. still i am waiting...
Jayapal Chandran
Hi, are you still with that stance? could you make a final comment for the above? that will be nice. thank you for all the above.
Jayapal Chandran
+1  A: 
$(function() {
  $('#okbuttonid').bind('click',function() { alert('here'); })
});

is fine, since the okbutton is allready in the DOM.

try

$(function() {
  $('#okbuttonid').live('click',function() { alert('here'); })
});

to bind the event to the document and the click event bubbles up to the document and triggers the function

a much better way is to use delegate:

$(function() {
  $('#parentOfokbutton').delegate('#okbutton','click',function() { alert('here'); })
});

this binds the event to 'parentOfokbutton' an is nearly the same as the 'live' function but the event don't has to bubble up the DOM

if you still want to use the bind function (for some reason) prepend an unbind methode:

$(function() {
     $('#okbuttonid').unbind('click').bind('click',function() { alert('here'); })
});
revaxarts
Hi, it is working. the last option. can you explain me about this document.onclick = function vs bind. ? could you please just take a look at the first answer and its comments. if you have time. thanks.
Jayapal Chandran
the bind function "binds" an event to an object with 'addEventListener'.So document.onclick attaches an event to the document and bind can bind events to (nearly) every element.in jQUery you can also bind custom events $('#myel').bind('myevent', function(){}); and trigger this with $('#myel').trigger('myevent');
revaxarts
oh.ok. what if i am creating a native js class without using jquery to show a dialog and to add a callback function to the ok buttons onclick. hope you can understand me. i have already explained in quesiton and in the other answers comments. suggestions will be fine.
Jayapal Chandran
the jquery bind() is just a wrapper function.IE doesnt know the addEventListener. It uses attachEvent instead. jQuery checks the client is IEjust use a function from above
revaxarts