0

How to change the following code to make use of all my 4 textboxes with id extra1, extra2,persons and tables?

$(document).ready(function () {
  $('#extra1').blur(function () {
        if ($.trim(this.value) == "") {
          $('#btnUpdate').attr("disabled", true);
        }
      else {
            $('#btnUpdate').removeAttr("disabled");
      }
    });
});

3 Answers 3

2
  $('#extra1').blur(function () {

to

  $('#extra1, #extra2, #persons, #tables').blur(function () {
Sign up to request clarification or add additional context in comments.

Comments

2

The code modification would be:

$(document).ready(function () {
  $('#extra1, #extra2, #persons, #tables').blur(function () {
        if ($.trim(this.value) == "") {
          $('#btnUpdate').attr("disabled", true);
        }
      else {
            $('#btnUpdate').removeAttr("disabled");
      }
    });
});

But, why not give all of them a class, and use that class as the selector? That way, you can easily add elements later without changing your Javascript.

1 Comment

That's true. Should I change it to .myclass?
1

Use jQuery delegate which will bind only one event handler but will act only on the element which triggers the event. Try this

Wokring demo

$(document).ready(function () {
  $('formSelector').delegate('input[type=text]', 'blur', function () {
      if ($.trim(this.value) == "") {
          $('#btnUpdate').attr("disabled", true);
      }
      else {
            $('#btnUpdate').removeAttr("disabled");
      }
    });
});

4 Comments

@Giorkouros - Take a look at this, its very simple. Replace the appropriate form selector in the code thats it.
This is my fiddle jsfiddle.net/mPuj9/1 with your jQuery script. I still didn't manage to do it.
It is very well working, tried your fiddle jsfiddle.net/mPuj9/1 too. Focus of any textbox, enter something and focus out of the textbox you will see the button gets enabled. What else do you need?
Please verify this and make it as an answer if it helped you.

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.