Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I empty an input value with jQuery?

I’m trying to make a modal dialog with images where you can select multiple images. I need to get values from an input and then to empty it, but I cannot empty the input. I tried .val('') and .val(null), but neither worked for me.

Here is the full code:

$("#hdselect").click(function(){         $(".modal").html("");          $.post('mediaservice.php',{hd:'ok',images:$("#hdimages").val()},function(data){             $(".modal").append(data);         });         $(".modal").dialog({             'modal':true,             'title':"Click the image to select",             'width':960,             'height':600,             'resizable':false,             'show': {effect: 'drop', direction: "up"},             'buttons': {"Ok": function() {                       var hd=Array();                     var hdval=$("#hdimages").val();                     $("#hdimages").attr('value',' ');                     $("input[name='hd[]']:checked").each(function(){                         hd.push($(this).val());                     });                     if(hdval!=''){                         hdval=hdval+","+hd;                     }else{                         hdval=hd;                     }                                             $("#hdimages").val(hdval);                     var images=$("#hdimages").val();                     $.post('mediaservice.php',{getHd:images},function(data){                         $("#imgthumbBase").append(data);                     });                     $(this).dialog("close");                 }             }         });     }); 

The idea is that the user clicks a button and a modal dialog opens with multiple images and checkboxes. At this point I need to get the values from an input, and then clear it.

like image 916
Vit Kos Avatar asked May 25 '12 12:05

Vit Kos


People also ask

How do you empty an object in jQuery?

jQuery isEmptyObject() method The isEmptyObject() method is used to determine whether the passed argument is an empty object or not. It returns a Boolean value. If it finds the passed value is an empty object, it returns true. Otherwise, it returns false.


2 Answers

To make values empty you can do the following:

 $("#element").val(''); 

To get the selected value you can do:

var value = $("#element").val(); 

Where #element is the id of the element you wish to select.

like image 53
Darren Avatar answered Oct 07 '22 10:10

Darren


You could try:

$('input.class').removeAttr('value'); $('#inputID').removeAttr('value'); 
like image 28
Brandon Ferrara Avatar answered Oct 07 '22 09:10

Brandon Ferrara