Connor Bishop
Connor Bishop

Reputation: 971

jQuery not getting changed input value

Ive got a button with id 'filter' and also two input fields:

<input type="text" name="Status" value="{{ $Status }}">
<input type="text" name="CusJobNum" value="{{ $CusJobNum }}">

Then I have this jQuery script

$(document).on('click', '#filter', function(){
    var url = 'JobsByDate/?';
    $('input').each(function(){
        var a = $(this).attr('name');
        var b = $(this).attr('value');
        if(!(b == ''))
            url = url + '&'+a+'='+b;
    });

    window.location = url;
});

The code is only getting the default value for the input even if i make a change to the field. Does anyone know why?

Upvotes: 0

Views: 62

Answers (3)

James
James

Reputation: 1446

Use val() to get value of input.

 $(document).on('click', '#filter', function(){
        var url = 'JobsByDate/?';
        $('input').each(function(){
            var a = $(this).attr('name');
            var b = $(this).val();
            if(!(b == ''))
                url = url + '&'+a+'='+b;
        });

        window.location = url;
    });

Upvotes: 0

Pranav C Balan
Pranav C Balan

Reputation: 115212

You are using attr() method to get the value which always return the attribute value in the markup. You should use val() method to get the current value of the element.

var b = $(this).val();

Upvotes: 1

Satpal
Satpal

Reputation: 133403

Use val() to get the value, as of now you are reading its attribute. the method .attr() is used to get attribute value

var b = $(this).val();

OR, Go native

var b = this.value;

Upvotes: 3

Related Questions