Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign null value to Hidden variable in javascript?

I have a hidden input variable called str.

I am assigning "abc" value to it.

Then I try to assign null value or let's say null reference to it. But I couldn't.

Edit

part of code.

Hidden Field...

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

I also use jQuery.

if ($(str).val() == "abc") {
     $("#str").val(null);
             }
like image 961
Vikas Avatar asked Dec 14 '22 04:12

Vikas


1 Answers

I'm not sure nulling the value is meaningful - you should either blank the value, or delete the whole field (not just the value).

Based on the example code you provided...

For example:

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

or

$("#str").remove()


Another option, if you may need to toggle the field on or off (so rather than deleting & re-creating) would be disabling the field - disabled fields don't get submitted with the form.

$("#str").attr('disabled','disabled')
and
$("#str").removeAttr('disabled')
like image 126
Peter Boughton Avatar answered Mar 06 '23 19:03

Peter Boughton