0

i have 3 input fields like this

<input type="text" id="from-price" name="from-price"/>
<input type="text" id="to-price" name="to-price"/>
<input type="text" id="price" name="price"/>

and I want that when the values entered in all the fields it will show a button So for that I have my code like this

var val1 = jQuery('#from-price').val().length;
        var val2 = jQuery('#to-price').val().length;
        var val3 = jQuery('#price').val().length;
if (val1 && val2 && val3  < 1 ) {
                jQuery('#add-fields').attr("disabled", false);
            }

But this is not working.

Update Every time I am getting length 0 why? Even after text entered?

2
  • 1
    You might want to listen to an event and run that code every time that event occurs. Commented Nov 30, 2013 at 11:42
  • Use console.log(jQuery('#from-price').val().length) in firefox / chrome to see what values they output Commented Nov 30, 2013 at 11:44

9 Answers 9

5
var elems = $('#from-price, #to-price, #price');

elems.on('keyup', function() {
    var hasValue = elems.filter(function() {
        return this.value.length
    }).length != elems.length;

    $('#add-fields').prop("disabled", hasValue);
});

FIDDLE

Sign up to request clarification or add additional context in comments.

Comments

3
var $i = $('input.prices').on('change', function(){
    var b = $i.filter(function() {
       return $.trim(this.value).length > 0; 
   }).length !== $i.length;

   $('#add-fields').prop("disabled", b);
});

Comments

1

Use .prop() to set the disabled state

jQuery(function ($) {
    var $from = $('#from-price'),
        $to = $('#to-price'),
        $price = $('#price'),
        $add = $('#add-fields');

    $from.add($to).add($price).change(function () {
        $add.prop('disabled', !($.trim($from.val()).length && $.trim($to.val()).length && $.trim($price.val()).length))
    });
})

Demo: Fiddle

2 Comments

If you could only minify the code - it would make a beginner developer's life much easier...
You know: jQuery(function(e){var t=e("#from-price"),n=e("#to-price"),r=e("#price"),i=e("#add-fields");t.add(n).add(r).change(function(){i.prop("disabled",!(e.trim(t.val()).length&&e.trim(n.val()).length&&e.trim(r.val()).length))})})
1

Replace your if condition with the following:

if (val1 > 0 && val2 > 0 && val3 > 0 ) {
    jQuery('#add-fields').attr("disabled", false);
}  

JSFIDDLE DEMO

7 Comments

I suspect he wants to check if val > 0 or similar instead, since he wants to enable only if all fields have something filled in.
@JoachimIsaksson OP's if condition is contain the syntax error that's the main issue.
@JoachimIsaksson: "@Dh.." seems to be correct in his interpretation
@abhitalks "I want that when the values entered in all the fields it will show a button"
@Dh...every time I am getting length 0 why?
|
0
    var val1 = jQuery('#from-price').val().length;
    var val2 = jQuery('#to-price').val().length;
    var val3 = jQuery('#price').val().length;

    if (val1 > 0 && val2 > 0 && val3  > 0 ) {
            jQuery('#add-fields').attr("disabled", "false");
    }

This should work.

Comments

0

Seems like you're checking the values only on page load. That's why it always shows 0 unless you prefill the form some way.

To make the check dynamic, put your code into a function and wrap it inside a change function:

$('input').change(my_function);

Of course, it would be great to add a class to those three inputs you have and then use a class selector (in case there are more inputs on the page): $('input.my_class')...

Also, your if clause can be something like this:

if (val1 > 0 && val2 > 0 && val3 > 0) {
    jQuery('#add-fields').attr("disabled", false);
}
else {
    jQuery('#add-fields').attr("disabled", true);
}

3 Comments

Op is asking about multiple conditions in if statement. The event and handler he probably has already figured out.
@abhitalks I'm not sure about that: "I want that when the values entered in all the fields it will show a button"
That is where he is explaining his use-case. Please see the question title for the problem he is facing.
0

I have put up a fix for this problem as part of this fiddle. Do let me know if that works for you.

http://jsfiddle.net/3STqm/

As part of the fiddle, the HTML code looks like:

<input type="text" id="from-price" name="from-price"/>
<input type="text" id="to-price" name="to-price"/>
<input type="text" id="price" name="price"/>
<button id="theButton">Submit</button>

And the jQuery code looks like:

var items = $("#from-price,#to-price,#price");

var button = $("#theButton").hide();

items.change(function(){
    var availableDataSize http://jsfiddle.net/3STqm/= items.map(function(){
        return $.trim($(this).val()) != "" ? true : null;
    }).length;                    
    availableDataSize == items.length ? button.show() : button.hide();
});

It simply tries to find the amount of data available and if that is equal to the number of fields, then show the button else hide it.

Comments

0

I am assuming that you want to enable the button if all the fields are filled, if so then your if condition is incorrect. It should be greater then 0 which indicates that the field is not empty.Try this:

if (val1 > 0 && val2 > 0 && val3 > 0 ) {
     jQuery('#add-fields').attr("disabled", false);
     }

Updated answer:If you want to check instantly, then you need to attach an event handler.Try this:

$("input").on("keyup",function(){
    var val1 = jQuery('#from-price').val().length;
    var val2 = jQuery('#to-price').val().length;
    var val3 = jQuery('#price').val().length;
if (val1 >0 && val2 > 0 && val3  > 0 ) {
            jQuery('#add-fields').attr("disabled", false);
}
});

Demo

2 Comments

@NewUser Because you need a listener in order to check the values on every change and not only on the page load.
@Joke_Sense10 it is fine when I am entering values for the first time but when I am removing the values by backspace then it should again disable the button but in your fiddle when I am removing the values still its showing the button in active?
-1
$(document).on('change','#from-price, #to-price, #price',function(){
    var val1 = jQuery('#from-price').val();
    var val2 = jQuery('#to-price').val();
    var val3 = jQuery('#price').val();
       if (val1!="" && val2!="" && val3!="" ) {
            jQuery('#add-fields').attr("disabled", false);
        }else{
            jQuery('#add-fields').attr("disabled", true); 
         }

});

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.