How to assign an empty value to a hidden variable in javascript?

I have a hidden input variable str.

I assign it the value "abc".

Then I try to assign a null value or give it a link to it. But I could not.

Edit

part of the code.

Hidden field ...

<input id="str" name="str" type="hidden" value="" /> 

I also use jQuery.

 if ($(str).val() == "abc") { $("#str").val(null); } 
+4
source share
2 answers

I'm not sure that the value of the value makes sense - you need to either delete the value or delete the entire field (and not just the value).

Based on the sample code you provided ...

For instance:

 $("#str").val('') 

or

 $("#str").remove() 


Another option, if you need to enable or disable a field (so that instead of deleting and re-creating it) would disable fields with disabled fields, they will not be sent along with the form.

 $("#str").attr('disabled','disabled') and $("#str").removeAttr('disabled') 
+12
source

Assign it an empty string. It will be handled the same way on the server side.

  var inp = document.getElementById('str'); inp.value = ''; // actually inp.value = null will work here 

Or using jQuery

  if ($(str).val() == "abc") { $("#str").val(''); } 
+1
source

All Articles