Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which way is efficient?

I have over 500 buttons on the page.

<button class="btn-x">test</button>

jQuery:

// #1
$('button[class*="btn-x"]').live('click', function() { }});

// #2
$('.btn-x').live('click', function() { }});

Which call is efficient now, calling directly by class or button[attr*], I want to know it, because it can cause problem later when I have more then 500 buttons but I am not sure if they affect the same result.

like image 346
Vural Avatar asked Dec 04 '22 13:12

Vural


1 Answers

The class selector will be an order of magnitude quicker.

That being said, live() is deprecated. You should be using delegate() or if you are using jQuery 1.7+ on()`.

For example:

// < jQ 1.7
$('#staticParent').delegate('.btn-x', 'click', function() { });

// jQ 1.7+
$('#staticParent').on('click', '.btn-x', function() { });
like image 175
Rory McCrossan Avatar answered Dec 22 '22 08:12

Rory McCrossan