1

I am looking for a way to add a button to a jQuery button-set at run-time with JavaScript.

        if(response.success) {
            var menuItem = document.createElement('a');

            $(menuItem).attr('href', response.href);
            $(menuItem).attr('category-id', response.id);
            $(menuItem).text(title);

            $("#menu-buttonset").append(menuItem);
        }

I am adding the hyperlink to the div container of the button-set using JavaScript, and then re-calling buttonset() on the container.

 $("#menu-buttonset").buttonset();

This however does not seem to rebuild the button-set as I expected.

I am not seeing any function in the jQuery API for this either.

Can anyone provide some insight?

Resolved:

Using refresh method as suggested, rebuilt the button-set.

$('#menu-buttonset').buttonset('refresh');
2
  • Is $(categoryItemSelector).parent() same as $("#menu-buttonset")? Commented Dec 24, 2010 at 5:04
  • Yes it is the same, the example has been updated to make this more clear. Commented Dec 28, 2010 at 3:41

2 Answers 2

2

You need to call the refresh method after adding an new item to the button set.

$("#menu-buttonset").append(menuItem).buttonset('refresh');

I cannot find any documentation for this method in jquery, but it is available.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I'm not sure how I missed that in the documentation.
Button API Documentation - Refresh is available now in documentation.
2

It seems to me like buttonsets use radiobuttons or checkboxes, not links:

http://docs.jquery.com/UI/Button

Take a look here: http://jsfiddle.net/Fe9Tx/1/ (Fixed it so selection works)

1 Comment

After several minutes of searching - this was the exact answer I was looking for. zsalzbank - thanks for taking the time!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.