1

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.

3
  • 3
    Why not select by ID instead of name? Commented May 14, 2014 at 16:59
  • It works like a charm jsfiddle.net/EptYy Commented May 14, 2014 at 17:00
  • @diodeus Sorry, I should have mention that '#fafsaNbr' would conflict. Although I have tried to get rid of that conflicting part, just to try it, and I am still getting it is undefined. Commented May 14, 2014 at 17:05

3 Answers 3

2

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

fafsaNbr = $("input[name='fafsaNbrFam']").val();
Sign up to request clarification or add additional context in comments.

1 Comment

single quote is optional
1

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?

1 Comment

Marking this as answered. It wasn't the scriptlet, but the placement of my input. Your post made me rethink that. The original input was being processed (I could take out the hidden and see it on the screen), but changing the location still somehow fixed it. Still unsure why.
0

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.

1 Comment

I get NaNX in my alert. Undefined still.

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.