4

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);
             }
2
  • 2
    Do you have some code you can paste into the question here? Commented May 12, 2009 at 13:30
  • 2
    Can we see some code, please? Commented May 12, 2009 at 13:30

2 Answers 2

12

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')
Sign up to request clarification or add additional context in comments.

1 Comment

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.
1

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('');
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.