views:

1390

answers:

2

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);
             }
+1  A: 

Assign the empty string to it. It will be treated 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('');
 }
tvanfosson
+5  A: 

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')
Peter Boughton
You can't assign NULL to an element's value--it is HTML. HTML can't differentiate from the keyword NULL and the string 'null' if its placed in the actual tag and, therefore, neither can jQuery in this context. Peter is right about his answer(s). If you want to return an empty string result when querying that element, use his first example. If you want to return 'undefined' (not a string), use his second example.
KyleFarris