Christoffer
Christoffer

Reputation: 3

jQuery - problem with buttons in content loaded from ajax

Im using the buttons from jQuery UI like this:

("button").button();

Simple..

But when i use the tag in a file loaded by ajax, the button script is not applied the button.

Ex: file.php:

<button>Testbutton</button>

When the this file is loaded on a page with ("button").button(); like this:

$("#updateDiv").load(file.php);

.. the button is not applied the jQuery UI script.

I've taken a look at the jQuery "live"-function, but it doesnt seem to work for me :(

Upvotes: 0

Views: 1878

Answers (4)

ivansabik
ivansabik

Reputation: 613

Note that the called Ajax page has to have the JQuery UI CSS file imported

Upvotes: 0

istvan.halmen
istvan.halmen

Reputation: 3350

You have to init your button in the callback function:

$("#updateDiv").load('file.php', function() { $('button').button(); });

Optionally you can use ajaxSuccess:

$('#updateDiv').ajaxSuccess(function() {
  $('button', this).button();
});

This will init all your buttons inside your div every time a load is successfuly called on your div. See http://api.jquery.com/ajaxSuccess/ for details.

Upvotes: 1

Rory McCrossan
Rory McCrossan

Reputation: 337610

Try calling the .button function in the callback of the AJAX request so that it is run on the new content placed in the DOM.

$("#updateDiv").load('file.php', function() {  
    $("button").button();
});

Upvotes: 0

Matt Ball
Matt Ball

Reputation: 359896

.live() doesn't work because it only handles UI events (click, dblclick, mousedown, etc.). You need to supply a callback to .load():

$("#updateDiv").load('file.php', function (){
    $(this).find('button').button();
});

Upvotes: 2

Related Questions