inBlackAndWhite
inBlackAndWhite

Reputation: 91

I can get input val(), but not name

$(".form").submit(function(e){
        e.preventDefault();
        $.ajax({
            url: "file.php",
            type: "POST",
            data: {
                input_value: $(".form").find("input:not(:first-child)").filter(function(){
                    return this.value.length !== 0;
                }).val(),

                input_name: $(".form").find("input:not(:first-child)").filter(function(){
                    return this.value.length !== 0;
                }).name,
            },
            success: function(data) {

            },
        });
    });

okay, so I'm submitting a form with several inputs, but only one with a value that isn't blank.

So, inside data, my 'input_value' variable is working fine, it finds the only input with a value and stores that value.

I try the same idea to get the input's 'name' attribute (inside input_name variable), but my php file gives me a 'Undefined index' error...

I don't understand why it works for the input's value, and not for its name...

oh, by the way, the inputs are generate dynamically (not sure if it has something to do with that...)

Thanks so much!!

Upvotes: 0

Views: 79

Answers (2)

adeneo
adeneo

Reputation: 318182

There is no .name property directly on a jQuery object, you have to use prop()

$(".form").find("input:not(:first-child)").filter(function(){
          return this.value.length;
}).prop('name'),

Upvotes: 2

j08691
j08691

Reputation: 207901

Change:

.name,

to:

.attr('name'), or .prop('name'),

Which one depends on the version of jQuery you're using. See .attr() and .prop()

Upvotes: 4

Related Questions