Mr Man
Mr Man

Reputation: 1588

jquery getting value from input

This has me stumped, and should be pretty simple. I have an input in my html:

<input type="text" id="fafsaNbrFam" name="fafsaNbrFam" value="<%=nbrFam%>" class="hidden" />
System.out.println(nbrFam);   // Works, gives me "6"

Then my js code:

$("#submit").click(function(e) {                               
        var numEntries = 0;
        var fafsaNbr = 0;

        $("input[name^='name_']").each(function() {
          if (this.value) {
            numEntries++;
          }
        });

        // EVERYTHING ABOVE HERE WORKS

        fafsaNbr = $("input[name=fafsaNbrFam]").val();
        alert(fafsaNbr + "X");

        // WHERE THE 6 is I want to put the variable fafsaNbr, just hardcoded for now.

        if (6 > numEntries && !confirm("The number of members you listed in your household is less than the number you indicated on your FAFSA.  Please confirm or correct your household size. ")) {
          e.preventDefault();
        }
      });

On my alert to test this, I get "undefinedX", so basically my jquery to get the value is coming up undefined.

EDIT: So it turns out my code wasn't the problem, but the placement of my input. Even though the original input placement was being processed, once I changed it, it all worked properly. Needless to say, I am still stumped.

Upvotes: 1

Views: 86

Answers (3)

mohamedrias
mohamedrias

Reputation: 18566

Your code is working fine,

I just added your code to jsFiddle and it works

Live EXAMPLE

Could you please make sure, the java scriplet is loading inside the value tag properly or not by checking the view source in browser?

Upvotes: 1

danieltmbr
danieltmbr

Reputation: 1042

Try to parse the value of the input like this:

    fafsaNbr = parseInt($("input[name=fafsaNbrFam]").val());

Or Check whether the $("input[name=fafsaNbrFam]") is undefined or not.

Upvotes: 0

Ballbin
Ballbin

Reputation: 727

You are missing the quotes around the name value. Try:

fafsaNbr = $("input[name='fafsaNbrFam']").val();

Upvotes: 2

Related Questions