简体   繁体   中英

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=''>

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 ""

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM