0

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?

1
  • 2
    use var b = $(this).val(); Commented Aug 4, 2016 at 6:40

3 Answers 3

3

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;
Sign up to request clarification or add additional context in comments.

Comments

1

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();

Comments

0

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;
    });

Comments

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.