2

I am new here and I have a simple (hopefully) problem at hand. I am trying to figure out how to switch classes on my "Apply" button once an end-user clicks inside a form field/element.

The demo test page I have up can be found here - http://www.iconpayment.com/test/Untitled-2.html

Also does having the image as a background or inline img src make a difference?

Thanks!

Noel

5 Answers 5

2
$('.button').click(function(event){
    $(this).removeClass('button').addClass('button-disabled');
});
Sign up to request clarification or add additional context in comments.

1 Comment

^ I mean in the removeClass function of course, not the first selector
1

In case you end up having more than one button with the same class name, it would be a good idea to address the button's ID when replacing the classes (using the same snippet as cpharmston):

$('#button-div').click(function(event){
    $(this).removeClass('button').addClass('button-disabled');
});

Comments

1

...I am trying to figure out how to switch classes on my "Apply" button once an end-user clicks inside a form field/element.

$('form > input').click(function() {
    $('#button-div').removeClass().addClass('button-disabled');
});

As a usability side-note, it might be a good idea to re-enable the button if the form inputs' 'blur' event is triggered and nothing has been edited (i.e. the form hasn't been made 'dirty').

Comments

0

To answer your first question...

Give all of your clickable form elements the class "affectsApply", or alternatively, just select them all via a selector (#formId input, #formId textarea, etc.)

then just use something like the following:

$(document).ready(function(){
  $('.affectsApply').click(function(){
    $(formElement).addClass('highlight');
  });
});

Comments

0

If you are wanting to switch to and from you can use toggle():

$(".toggleButton").toggle(
    function() {
        $(this).removeClass("enabled").addClass("disabled");
    },
    function() {
        $(this).removeClass("disabled").addClass("enabled");
    }
);

Of course this does not track the state in anyway, so you might want a flag or something to track that if needed.

Example here: http://docs.jquery.com/Events/toggle

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.