stormdrain
stormdrain

Reputation: 7895

strange behavior on display|display:none elements

I have a custom modal dialogue that consists of a simple div and some css. There are 2 buttons (OK, CANCEL) buttons. The CANCEL button is always the same; it hides the modal dialogue via onclick="$('#div').css('display','none')" (NB: this is also how the modal is shown; ('display','')). I assign different actions to the OK button depending on the need. This is done via $('#okBTN').attr('onclick','my_function()').

It works, but only the first time ©

The first time I open the modal and walk through the steps, everything works as expected. If I close the modal, however, then re-open it, the OK button has no action on it. I mean, the onclick is assigned (correctly); it's in the source code, and it will alert correctly via .attr('onclick'), but clicking the button does nothing. I have it set that when the modal pops up, the onclick is assigned each time; but it's almost as if there is a shadow copy or something stuck in memory or the DOM. Although, I don't see anything strange in Firebug....

I've tried cloning the button, reassigning it, then replaceWith'ing. I've also tried remove'ing it and re-adding it...

Any clues?

Upvotes: 1

Views: 221

Answers (3)

Adam Terlson
Adam Terlson

Reputation: 12730

Hate to say it my friend but you're not leveraging the benefits of jQuery.

  • Why set display via CSS? Just use .hide() .show() or .toggle().
  • Why are you setting on onclick attribute via javascript? This doesn't make much sense at all. Use $(elem).click(my_function);

The second bullet will likely fix your problem, but I'd do some serious re-evaluation.

Good luck!

Upvotes: 2

Blender
Blender

Reputation: 298384

I'd try using .css('display','block') instead of .css('display',''), as assigning a blank display value doesn't seem like a good idea (it might work, but just to be safe).

Have you tried setting the .bind() function of the element?

$('#okBTN').bind('click', my_function);

Upvotes: 0

marcosfromero
marcosfromero

Reputation: 2853

Use bind or event-name binders:

$('#okBTN').click(my_function)

Upvotes: 0

Related Questions