954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

jQuery delay function

Hi,

I want to change color of a element after some delay:
For. e.g:
Initially div is in Orange Color.
Then onClick it will become White
then after half second delay, it will become Black.
But it is not working.
It becomes black on the first click & White on second
Code:

<script type="text/javascript">
$(".tabButton").click(function() {
			$(this).css("background-color", "white").delay(500, function() {
				$(this).css("background-color", "black");
			})
		});
</script>
<style>
.tabButton {
	float: left;
	position: absolute;
	width: 50px;
	height: 20px;
	top: 150px;
	border-width: 1px;
	border-style: solid;
	border-color: black;
	text-align: center;
	font-style: italic;
	background-color: orange;
}
</style>


Thanks

raul8
Light Poster
37 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

before you begin the delay, try "capturing/recording" a reference to " this " in some variable so that your delayed function actually knows what " this " means:

$(".tabButton").click(function() {
var self=this;
			$(self).css("background-color", "white").delay(500, function() {
				$(self).css("background-color", "black");
			})
		});
</script>
hielo
Veteran Poster
1,124 posts since Dec 2007
Reputation Points: 116
Solved Threads: 244
 

this will work

$(".tabButton").click(function() {
 $(this).css("background-color", "white").delay(500).queue(function () {
                    $(this).css("background-color", "black")
                    $(this).dequeue();
                });
 
});
ckchaudhary
Newbie Poster
23 posts since Oct 2011
Reputation Points: 12
Solved Threads: 5
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: