2

I have a submit button which has a color red css style by default. How can I apply a css styling on it on the fly and seeing the result without refreshing the page using jquery?

I have an oncheck query and I want to put the code that will change styling above the return true;

My button

<input type="submit" name="submit" id="submitForm" class="btn btn-primary-alt" value="Submit" onsubmit="return validate_age();" />

The css styling I want to apply on my submit button

.btn-primary-alt:hover {
background-color: #3b8410;
border-bottom: 3px solid #2a620a;
border-color: #2a620a;
color: #fff;
}

My jquery

if($("#age_18").is(":checked"))
{ 
  // This is where I want the css styling will happen.
  return true;
}
else
{
  alert("You must be above 18 years of age.");
  return false;
}

So basically when my checkbox is checked I want to add a css styling on my submit button. thanks

2
  • 1
    If you return true in the very next line the page will go to the new page before the changed CSS is visible. Do you wish to prevent the page-reload as well as style the check-box; or do you want the changed CSS to be visible and then reload the page (or go to the next page, etc...)? Commented Jun 1, 2015 at 12:19
  • Do it on-change of the radio button. What you are trying (assuming a document.ready) will fire only during the page load OR when submitting the page. Commented Jun 1, 2015 at 12:20

5 Answers 5

1

You can declare your css like this

.btn-primary-alt-hover,.btn-primary-alt:hover {
   background-color: #3b8410;
   border-bottom: 3px solid #2a620a;
   border-color: #2a620a;
   color: #fff;
}

and then you can apply this on your button like this

$("#submitForm").addClass('btn-primary-alt-hover');
Sign up to request clarification or add additional context in comments.

Comments

0

Best solution would be to create a new class (not :hover). So for example

.checked{
 background-color: #3b8410;
 border-bottom: 3px solid #2a620a;
 border-color: #2a620a;
 color: #fff;
}

Then use jQuery to add or remove the class.

$('#submitForm').addClass('checked');

and

$('#submitForm').removeClass('checked');

1 Comment

Wow. This is what I exactly need. I'm about to do the same thing later which is to remove the class if the user unchecked the box. Thank for all your help. I wish I could tick all your answers for it works but I only need to choose one :D
0
$("#age_18").change(function(){
  if($("#age_18").is(":checked")) {
    // Add Your Styling Here to Submit Button...
    return true;
  } else {
    alert("You must be above 18 years of age.");
    return false;
  }
})

Your Logic is Perfect only one thing.

You have to call it's Change Function to check if the checbox is checked or not..

Comments

0

I would keep the styles away form the CSS as you have it in your separate CSS file anyway.

I'd extend your CSS from: .btn-primary-alt:hover to .btn-primary-alt-active, .btn-primary-alt:hover so that you can apply a class through jQuery to apply the styles. Your JS would look like:

// This is where I want the css styling will happen. $('.btn-primary-alt').addClass('btn-primary-alt-active');

Comments

0
$("#submitForm").addClass("Add your class name");

in your style sheet .the-above-class-name { background-color:[Some color]; color:[Some color]; }

OR

$("#submitForm").css({"background-color":"some color", "color":"some color"});

You can look at W3Schools for reference if you want to learn more about it http://www.w3schools.com/jquery/jquery_css.asp http://www.w3schools.com/jquery/jquery_css_classes.asp

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.