3

I have created one dialog box in jquery. and there is one button called 'save'. I need to add one id to this save buttton. How can I achive in this in jquery. This is my code

$(function() {
    $( "#dialog" ).dialog({

         height: 400,
      width: 650,
      modal: true,
      buttons: {

        Save: function() {
          dialog.dialog( "close" );
        }
      },
      close: function() {
        form[ 0 ].reset();
        allFields.removeClass( "ui-state-error" );
      }

    });
});
Shaunak D
  • 20,588
  • 10
  • 46
  • 79
jumban
  • 59
  • 1
  • 7

5 Answers5

2

The Save function has a parameter event which has a target that is the DOM element of the button, then you can set the id inside of the function like this:

Save: function(event) {
   $(event.target).attr('id', 'your-id');
}

The specification about the buttons property says:

Specifies which buttons should be displayed on the dialog. The context of the callback is the dialog element; if you need access to the button, it is available as the target of the event object.

ScientiaEtVeritas
  • 5,158
  • 4
  • 41
  • 59
2

This is the most simplest

$(selector).attr('id', 'TheID');
Rush.2707
  • 685
  • 10
  • 29
1

try this one:

$(element).attr('id', 'YourNewID');
Ivin Raj
  • 3,448
  • 2
  • 28
  • 65
1
$(function() {
    $( "#dialog" ).dialog({
      height: 400,
      width: 650,
      modal: true,
      buttons: {
        save: {
          text: "Save",
          id: "my-button-id",
          click: function(){
            dialog.dialog( "close" );
          }   
        }
      }
      ...
    });
});
GeekPlux
  • 66
  • 5
  • Thanks a lot for your valid reply. I have one more doubt that how can I access this id in my js file.$("#rm-button-id").click(function(){ alert("here"); }); – jumban Jul 15 '16 at 05:52
  • This is what I am using now. – jumban Jul 15 '16 at 05:52
0

Use $("button").attr("id","testid");

Mani
  • 2,675
  • 2
  • 20
  • 42