tags:

views:

58

answers:

2

I have a dialog in which there are 2 textboxes. I want to reset the textboxes values once the dialog is destroyed. How can I do that?

If I have entered values in the textboxes and clicked cancel, reopening the dialog will show the data entered previously.

function setDialogWindows($element) {
      $('#change').dialog({    
           autoOpen: true,    
           width: 380,    
           buttons: {   
              "Cancel": function() {    
                   $(this).dialog('destroy');   
              },    
              "Accept": function() {    
              } 
     }): 
 }

$('#link').click(function() {    
    setDialogWindows('#change');    
});

<div id="change" title="Change password" >
    <input type="hidden" id="User_Name" name="Name"/>    
    <input type="textbox" id="text1" />
    <input type="textbox" id="text2" />
</div>
+2  A: 

At first, type textbox does not exist. Use text or password (for masked characters) instead.

Use this code to clean the inputs:

"Accept": function() {    
    $("#text1").val('');
    $("#text2").val('');
}

Finally, I've rewritten the code:

HTML:

<a href="javascript:void(0);" id="link">Show me a dialog!</a>
<div id="change" title="Change password">
    <input type="hidden" id="User_Name" name="Name"/>    
    <input type="text" id="text1"/>
    <input type="text" id="text2"/>
</div>

JavaScript:

$('#link').click(function() {    
    setDialogWindows();    
});

function setDialogWindows() {
    $('#change').dialog({
        autoOpen: true,
        width: 380,    
        buttons: {   
            "Cancel": function() {    
                $(this).dialog('close');
            },    
            "Accept": function() {
                /* Place your handler here. */
                $(this).dialog('close');
            }
        },
        close: function() {
                $("#text1").val('');
                $("#text2").val('');
        }
   });
}

You can use $(this).dialog('destroy'); with "Cancel" button, but it will not modify yout input fields as they are declared independently from the dialog. That's why you ought to clean them manually.

Bar
Are the textboxes not supposed to be empty after the destroy and when reopening the dialog?
@user281180, no. Destroying just *hides* the dialog.
Bar
What is the difference between closing and destroying? I want to reset the controls in the dialog when re-opening, how can I achieve that, without writing any other code? Is that possible?
Sorry, my bad. I've messed up with it. Destroying **should** clear inputs — i.e. destroying will return the element back to its pre-init state
Bar
I`m confused. I`m using destroy, still I have to clear the text values before closing?
If I use only $(this).dialog('destroy');, then next time the dialog doesn`t reopen again! That`s why I had wtitten the above code.
@user281180 Try modified version, please.
Bar
A: 

try

"Cancel": function() {
    $(':text',this).val('');    
    $(this).dialog('destroy');   
}

and also I notice, you missed 1 } on buttons :{} and <input type="textbox" id="text1" /> should be <input type="text" id="text2" />. notice the type...

edit

when you want to reset text boxes on re-open, don't need to destroy it...

$( "#change" ).dialog({
   open: function(event, ui) {
      $(ui).find(':text').val('');
   },
   autoOpen: true,    
   width: 380,    
   buttons: {   
      "Cancel": function() {    
            $(this).dialog('destroy');   
       },    
       "Accept": function() {    
       } 
   }
});
Reigel