How to add id in jquery?

前端 未结 5 552
無奈伤痛
無奈伤痛 2021-01-06 13:31

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 c

相关标签:
5条回答
  • 2021-01-06 13:33

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

    0 讨论(0)
  • 2021-01-06 13:48
    $(function() {
        $( "#dialog" ).dialog({
          height: 400,
          width: 650,
          modal: true,
          buttons: {
            save: {
              text: "Save",
              id: "my-button-id",
              click: function(){
                dialog.dialog( "close" );
              }   
            }
          }
          ...
        });
    });
    
    0 讨论(0)
  • 2021-01-06 13:51

    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.

    0 讨论(0)
  • 2021-01-06 13:51

    This is the most simplest

    $(selector).attr('id', 'TheID');
    
    0 讨论(0)
  • 2021-01-06 13:59

    try this one:

    $(element).attr('id', 'YourNewID');
    
    0 讨论(0)
提交回复
热议问题