Which way is efficient?

Vur*_*ral 4 html javascript jquery jquery-selectors

I have over 500 buttons on the page.

<button class="btn-x">test</button>
Run Code Online (Sandbox Code Playgroud)

jQuery:

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

// #2
$('.btn-x').live('click', function() { }});
Run Code Online (Sandbox Code Playgroud)

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.

Ror*_*san 9

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() { });
Run Code Online (Sandbox Code Playgroud)