0

I just can't get this to work.

HTML:

<form action="index.php?page=n0402" method="post" Name="AddPolish" >
    <div id="frmBrandInput">
        <label for="frmBrand">Brand name:</label>
        <input type="text" name="frmBrand" size="50" onkeypress="BrandCheck();" maxlength="100" id="frmBrand" value="Enter existing or new brand"  />
        <span id="frmBrand_Status"></span>
    </div>
</form>

Javascript:

function BrandCheck()
{
    var jsBrandName = document.forms["AddPolish"]["frmBrand"].value;
    alert(jsBrandName);
}

Why can't I get the value of frmBrand into jsBrandName? If I use Firebug to debug, I get the following in my Watch list: Watch Expression:

document.forms["AddPolish"]["frmBrand"];

Result:

NodeList[input#frmBrand property value = "G" attribute value = "Enter existing or new brand", input#frmBrand attribute value = ""]

I can see the value "G" which is what I entered in the input field. What am I doing wrong in passing it into the jsBrandName?

3
  • 1
    Is that really the actual HTML? Are there more than one "frmBrand" <input> elements? That's why the value is a NodeList and not a simple single input element. Commented Dec 30, 2013 at 23:17
  • 2
    does it work here: jsfiddle.net/uFp3V ? It works for me. Commented Dec 30, 2013 at 23:20
  • 1
    I don't think action='index.php?page=n0402' will work, with a $_GET URL like ?page=n402. Use <input type='hidden' name='page' value='n0404' /> instead. Commented Dec 30, 2013 at 23:27

1 Answer 1

1

The output you got implies that there are two inputs with name="frmBrand" in the form. You need to index them, e.g.

var jsBrandName = document.forms["AddPolish"]["frmBrand"][0].value;

to get the value of the first one.

Sign up to request clarification or add additional context in comments.

1 Comment

<insert lots of swearing> You were right. Somewhere way down in the html page, I had forgotten to change a frmBrand ID after copy/pasting. Thank you, it works now!

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.