7

I have a button which creates text boxes dynamically and there is another button 'Clear'.

If there is no text in any of the text field then the clear button is disabled or else it will be enabled. It works for the text box that is created when the dom is loaded but for the dynamically created textboxes it does not work.

Here is the HTML

<input type="button" value="Click Me" class="a" />
<input type="button" value="Clear" class="a" id="clearBasicSearch" />
<div id="basicSearchFields">
    <input type="text" />
</div>

Javascript

$(".a").click(function () {
    $("#basicSearchFields").append("<input type='text' class='b' />");
});

/*$(".b").live("keyup", function () {
        //alert('you pressed ' + $(this).val());
        $(this).val($(this).val().toUpperCase());
        });*/

var toValidate = $("#basicSearchFields input[type='text']");
$("#clearBasicSearch").removeClass('hidden').removeClass('button').attr('disabled', true);

toValidate.live('keyup', function () {
    console.log("hi");
    var valid = false; //default is false
    toValidate.each(function () {
        if ($(this).val().length > 0) {
            valid = true; //non-empty element found
            return false; //break
        }
    });
    $("#clearBasicSearch").attr('disabled', !valid).toggleClass('button', valid);
});

JSfiddle link - http://jsfiddle.net/TpExS/

Please help me out!!

4
  • I can't use 'on' as I am using lower version of jquery Commented Aug 28, 2013 at 11:52
  • 1
    Upgrade your jquery version Commented Aug 28, 2013 at 11:53
  • BTW, as of jQuery 1.7, the .live() method is deprecated. Use .on() instead ! api.jquery.com/on Commented Aug 28, 2013 at 11:57
  • 1
    Even if you can't use .on(), if you're using >= 1.4.2 you can and should use .delegate() instead of .live(). Commented Aug 28, 2013 at 12:09

5 Answers 5

20

Try this

$(document).on('keyup', "#basicSearchFields input[type='text']",function () {
    console.log("hi");
    var valid = false; //default is false
    var toValidate = $("#basicSearchFields input[type='text']");
    toValidate.each(function () {
        if ($(this).val().length > 0) {
            valid = true; //non-empty element found
            return false; //break
        }
    });
    $("#clearBasicSearch").attr('disabled', !valid).toggleClass('button', valid);
});
Sign up to request clarification or add additional context in comments.

2 Comments

I can't use 'on' as I am using a lower jquery version. I even can't update the jquery version as it might break other functionality.
Also can we create a function called 'validate' and do the stuff?
2
$(document).on('keyup', "input#basicSearchFields",function () {  
    // do something here
}

Comments

1

Here is another possible solution http://jsfiddle.net/TpExS/2/

Note: JQuery 2.0.2 used

Javascript

$(document).ready(function(){
    var fields = $('input[class=b]');
    for(var i = 0; i < fields.length; i++){
        $(fields[i]).on('keyup', validateFields);
    }
});

$(".a").click(function () {
    var newField = $('<input/>').attr('type', 'text').attr('class', 'b');
    $(newField).on('keyup', validateFields);

    $("#basicSearchFields").append(newField);
});

function validateFields(){
    if($(this).val().length){
        $('#clearBasicSearch').attr('disabled', false);
        return;
    }

    $('#clearBasicSearch').attr('disabled', true);
    var fields = $('input[class=b]');
    for(var i = 0; i < fields.length; i++){
        if($(fields[i]).val().length){
            $('#clearBasicSearch').attr('disabled', false);
            return;
        }
    }
}

Comments

1

Try

$("#basicSearchFields").delegate("input[type='text']", 'keyup', function () {
    validate();
});

function validate(){
    console.log("hi");
    var valid = false; //default is false
    var toValidate = $("#basicSearchFields input[type='text']");
    toValidate.each(function () {
        if ($(this).val().length > 0) {
            valid = true; //non-empty element found
            return false; //break
        }
    });
    $("#clearBasicSearch").attr('disabled', !valid).toggleClass('button', valid);
}

Demo: Fiddle

5 Comments

there is no 'live' in new jQuery versions try 'on'
@SreekeshOkky it is there in version 1.6 which is used by OP
Can we create a function as validate and do the same stuff?
use $("#basicSearchFields input:text")
If the OP is using 1.6 you should modify your code to use .delegate() rather than .live().
1

Simple, Updates your global "toValidate" variable when you add more elementson the click.

$(".a").click(function () {
    $("#basicSearchFields").append("<input type='text' class='b' />");
    toValidate = $("#basicSearchFields input:text");
});

Why? The time you set toValidate, it will return an array at that moment, you should updates it in each time there are new elements, or check for all of then inside the live event.

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.