I have multiple buttons that I use when people answer a question. How would I get the buttons to change colors when a person clicks on the button? I do not know jquery, I have b
$('input[type="submit"]').click(function(){
$(this).css('color','red');
});
Use class, Demo:- http://jsfiddle.net/BX6Df/
$('input[type="submit"]').click(function(){
$(this).addClass('red');
});
if you want to toggle the color each click, you can try this:- http://jsfiddle.net/SMNks/
$('input[type="submit"]').click(function(){
$(this).toggleClass('red');
});
.red
{
background-color:red;
}
Updated answer for your comment.
http://jsfiddle.net/H2Xhw/
$('input[type="submit"]').click(function(){
$('input[type="submit"].red').removeClass('red')
$(this).addClass('red');
});
I would just create a separate CSS class:
.ButtonClicked {
background-color:red;
}
And then add the class on click:
$('#ButtonId').on('click',function(){
!$(this).hasClass('ButtonClicked') ? addClass('ButtonClicked') : '';
});
This should do what you're looking for, showing by this jsFiddle. If you're curious about the logic with the ?
and such, its called ternary (or conditional) operators, and its just a concise way to do the simple if logic to check if the class has already been added.
You can also create the ability to have an "on/off" switch feel by toggling the class:
$('#ButtonId').on('click',function(){
$(this).toggleClass('ButtonClicked');
});
Shown by this jsFiddle. Just food for thought.
Use css:
<style>
input[name=btnsubmit]:active {
color: green;
}
</style>
You have to include the jquery framework in your document head from a cdn for example:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
Then you have to include a own script for example:
(function( $ ) {
$(document).ready(function(){
$('input').click(function() {
$(this).css('background-color', 'green');
}
});
$(window).load(function() {
});
})( jQuery );
This part is a mapping of the $ to jQuery, so actually it is jQuery('selector').function();
(function( $ ) {
})( jQuery );
Here you can find die api of jquery where all functions are listed with examples and explanation: http://api.jquery.com/
Add this code to your page:
<script type="text/javascript">
$(document).ready(function() {
$("input[type='submit']").click(function(){
$(this).css('background-color','red');
});
});
</script>