1

I am creating a jQuery UI dialog programmatically, as in

$( "#dialog" ).dialog({
        autoOpen: false,
        width: 400,
        buttons: [
            {
                class: "myButton",
                text: "Ok",
                click: function() {
                    $( this ).dialog( "close" );
                }
            },
            {
                text: "Cancel",
                click: function() {
                    $( this ).dialog( "close" );
                }
            }
        ]
    });

When the dialog is created, however, the button has a list of default classes assigned to it, and the label s wrapped in a SPAN.

<button type="button" class="myButton ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false">
     <span class="ui-button-text">Ok</span>
 </button>

I'd rather the button just have myClass, period, and if possible no span. As in:

 <button type="button" class="myButton">Ok</button>

How can I accomplish this?

2
  • Don't use jquery ui. Do your own css. Edit: I meant don't create the buttons using jquery dialog and add your own buttons. Commented Jun 10, 2014 at 17:58
  • That's how jQuery UI works, it uses all those classes internally. If they interfere with your application, you're doing something wrong. Commented Jun 10, 2014 at 18:00

2 Answers 2

2
$( "#dialog" ).dialog({
  autoOpen: false,
  width: 400,
  open: function(){
    $(this).next().find("button").removeClass("ui-button ui-corner-all ui-widget");
  },
  buttons: [
    {
      class: "myButton",
      text: "Ok",
      click: function() {
        $( this ).dialog( "close" );
      }
    },
    {
      text: "Cancel",
      click: function() {
        $( this ).dialog( "close" );
      }
    }
  ]
});

"create" function can be used instead of "open" function

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

Comments

0

There are 3 easy ways to accomplish this: (just use one of the following)

1 Just create the button as an html string in your code:

EX: $("#someelem").html("<button type=\"button\" id=\"myid\" class=\"myclass\">Ok</button>");

2 Download jQuery UI non-compressed and change the code so that the output is just the class you pass and does not add a span to the html.

EX FROM jQuery UI Code:

var buttonElement = this.buttonElement.removeClass( typeClasses ),
            buttonText = $( "<span></span>", this.document[0] )
                .addClass( "ui-button-text" )
                .html( this.options.label )
                .appendTo( buttonElement.empty() )
                .text(),
            icons = this.options.icons,
            multipleIcons = icons.primary && icons.secondary,
            buttonClasses = [];

Just remove the span tags so that buttonText = this.options.label; or something similar.

3 Change the class and button html with JavaScript after creation. EX: $(".myButton").attr("class","myButton").html("Ok");

1 Comment

Good approaches. I'll see which works best, thanks.

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.