How to clear input value using jQuery?

I am trying to make a modal dialog with images where you can select multiple images. I need to get the values ​​from the input and then delete it, but I cannot clear the input. I tried .val('') and .val(null) but didn't work for me.

Here is the complete 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 presses a button and a modal dialog box opens with several images and check boxes. At this point, I need to get the values ​​from the input, and then clear it.

+45
javascript jquery html jquery-ui
May 25 '12 at 12:59 a.m.
source share
6 answers

You can try:

 $('input.class').removeAttr('value'); $('#inputID').removeAttr('value'); 
+49
May 25 '12 at 13:02
source share

To make the values ​​empty, you can do the following:

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

To get the selected value, you can:

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

Where #element is the identifier of the element you want to select.

+73
May 25 '12 at 13:01
source share

The best way:

 $("#element").val(null); 
+15
Jul 21 '15 at 1:24
source share

The usual way to empty a text field using jquery is:

 $('#txtInput').val(''); 

If the code above does not work, check that you can get an input element.

 console.log($('#txtInput')); // should return element in the console. 

If you are still facing the same issue, submit your code.

+13
May 25 '12 at 13:07
source share

Another way:

 $('#element').attr('value', ''); 
+5
May 11 '15 at 13:34
source share
 $('.reset').on('click',function(){ $('#upload input, #upload select').each( function(index){ var input = $(this); if(input.attr('type')=='text'){ document.getElementById(input.attr('id')).value = null; }else if(input.attr('type')=='checkbox'){ document.getElementById(input.attr('id')).checked = false; }else if(input.attr('type')=='radio'){ document.getElementById(input.attr('id')).checked = false; }else{ document.getElementById(input.attr('id')).value = ''; //alert('Type: ' + input.attr('type') + ' -Name: ' + input.attr('name') + ' -Value: ' + input.val()); } } ); }); 
+1
Dec 19 '15 at 17:23
source share



All Articles