JQuery: Replace onclick event

I have the following code:

  $('#tableA').find("#expand_" + row_id).prop("onclick", null);
  $('#tableA').find("#expand_" + row_id).prop("onclick", 'expandAndShow('+row_id+')');
  $('#tableA').find("#expand_" + row_id).removeClass('icon-plus-sign');
  $('#tableA').find("#expand_" + row_id).addClass('icon-ok-sign');

I wanted to replace the previously linked method onlickwith a new one. This does not work. However, removeClassthey addClasswork well. Did I miss something?

+4
source share
2 answers

To remove an inline attribute using jQuery:

$('#tableA').find('#expand_' + row_id).removeAttr('onclick');
However, for IE <9, you should use:
.prop('onclick', null);

As explained in the docs .
However, I wonder why you are using findwith an identifier selector. I believe that I am right in saying findreturns an array of jQ objects. Not a single DOM object.
Maybe:

$('#expand_' + row_id).prop('onclick', null);

. onclick , 2 prop , :

$('#expand_' + row_id).prop('onclick', 'expandAndShow('+row_id+')');

onclick, . , :

$('#tableA').on('click', '*[id^=expand_]', function()
{
    alert($(this).attr('id').replace('expand_',''));
});

#tableA, , expand_. id, expand_.

+4

$('#tableA').find("#expand_" + row_id).unbind('click');
$('#tableA').find("#expand_" + row_id).on('click',expandAndShow(row_id));
+3

All Articles