jumban
jumban

Reputation: 59

How to add id in jquery?

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" );
      }

    });
});

Upvotes: 3

Views: 8593

Answers (5)

Rush.2707
Rush.2707

Reputation: 683

This is the most simplest

$(selector).attr('id', 'TheID');

Upvotes: 2

ScientiaEtVeritas
ScientiaEtVeritas

Reputation: 5278

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.

Upvotes: 2

GeekPlux
GeekPlux

Reputation: 66

$(function() {
    $( "#dialog" ).dialog({
      height: 400,
      width: 650,
      modal: true,
      buttons: {
        save: {
          text: "Save",
          id: "my-button-id",
          click: function(){
            dialog.dialog( "close" );
          }   
        }
      }
      ...
    });
});

Upvotes: 1

Ivin Raj
Ivin Raj

Reputation: 3429

try this one:

$(element).attr('id', 'YourNewID');

Upvotes: 1

Mani
Mani

Reputation: 2655

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

Upvotes: 0

Related Questions