Sartorialist
Sartorialist

Reputation: 301

Assign Empty Value to Input Field

I'm working on a Chrome Extension where I need to check whether a field in a form submission exists and is not blank, and if so, add that to another form (in lieu of doing a post request).

I'm checking whether it exists and is not blank here:

if(document.getElementById("txtBaseAnn").value !== '' && document.getElementById("txtBaseAnn").value !== null) {
                txtBaseAnn = document.getElementById("txtBaseAnn").value 
            } else {
                txtBaseAnn = '';
            }

And I'm using that variable to build a form like this:

newForm += "<input type='text' name='txtBaseAnn' value='" + txtBaseAnn + "'>";

If there's a value in the form, it works. If it's blank, however, what I get is this:

<input type='text' name='txtBaseAnn' value>

When what I want is this:

 <input type='text' name='txtBaseAnn' value=''>

Upvotes: 0

Views: 781

Answers (1)

stackoverfloweth
stackoverfloweth

Reputation: 6917

Seems to me your issue is that you're trying to select your element with getElementById but you are using name not id.

If you were using id correctly, then it wouldn't matter if value was written as <input type='text' id='txtBaseAnn' value>

The result of document.getElementById("txtBaseAnn").value would be ""

Upvotes: 1

Related Questions