It’s the little things on sites that often make you want to return. I am a massive fan of sites that feel light and free. Where when you click on things, it does things you don’t always expect or aren’t programmed in your mind to accept will happen. Things where you think “That’s a nice touch, why didn’t I think of that?”
So onto my point, here is a little touch that I often add to sites that I like to see happen. It’s simply having a ‘Back to top’ button at the foot of the page that once clicked, scrolls you up with an animation rather that a page refresh or a straight jerk to the top again.
Using the jQuery scrollTo plugin, you can achieve, what I see to be, a sweet scrolling animation.
Implementing scrollTo to scroll to top using jQuery
I’ll briefly outline the steps you will need to take to implement this, but more documentation and some better example can be found on the plugin site.
1. Download the js file that you will need and stick it in your site somewhere.
2. Include the js file into the page you want it on.
3. Initialise the function, so that when the link is clicked, it will perform the animation you want, with the speed you set and where you want it to scroll to. Then add the link you want to move you to the top.
Here is an example of the code you might use to do this:
<script type=“text/javascript” src=“js/jquery.scrollTo.js”></script>
<script type=“text/javascript”>
$(document).ready(function()
{
// scroll to top
$(‘a.topOfPage’).click(function(){
$.scrollTo( 0, 500);
return false;
});
});
</script><a href=“#” class=“topOfPage”>Top of page</a>
Here I have used jQuery to initialise the function when the a link with the class ‘topOfPage’ is clicked.
Once this is clicked, it uses the scrollTo plugin to go to the top of the page (as I have used 0 for the position) and with a speed of 500 milliseconds.
Arguments accepted
scrollTo accepts the following arguments:
$.scrollTo( target, duration, settings );
Target can either be an element such as an id of an element (#elementName) or a class (.className) or simply the position on the page you want (’44′, ’100px’, ‘+=30px’, etc).
Duration is the length of time in milliseconds that you want it to take to reach the destination.
Settings is basically a list of other parameters that you wish to pass to it, more information on these can be found here.
Example using scrollTo
An example of how I have implemented it in a site can be seen here. Just scroll down to the bottom and click the ‘Top’ link and it should scroll up nicely for you.
Like I said, it’s attention to detail like this on sites that I really like to see. You can take this how you like and use it to scroll up, down, across etc.
I’m going to be putting a lot of that into the making of a personal project Betheiddleman.com. Things like live validation, thickbox login boxes and more. So watch this space…





18 comments on “jQuery clear focus function”
I do this one better: by positioning the label over the field instead of putting the label in the field as text, I can leave the label there even while the field has focus, hiding it only when the user actually enters text. See for example the search field at http://foneshow.com/
That’s a different solution. I’m not sure it’s that intuitive though. I think I would expect the field to clear when I clicked inside it so I could start typing. I find myself trying to select the label and clear it rather than start typing over it.
Instead of creating the variable and doing this:
$(this).val(clearMePrevious);
you could just do this:
$(this).val() = $(this).attr(’title’);
@eep
Yes I could have done that….I’m not all too sure why I didn’t as I have used ‘attr’ to get the title already…strange
@eep
Thanks for this example – I’m just getting started with JQuery so this kind of thing really helps.
After some fiddling around I found that this didn’t work for me:
$(this).val() = $(this).attr(‘title’);
But this did:
$(this).val($(this).attr(‘title’));
Thnaks for that!I had the same issue with one of my sites!I love Internet!
Thanks, helped me a ton
.
One of the great features of jQuery is the ability to chain events. To save on a few bytes of characters the entire script can be condensed to:
$(document).ready(function () {
$(“.searchinput”).focus(function () {
if ($(this).val() === $(this).attr(“title”)) {
$(this).val(“”);
}
}).blur(function () {
if ($(this).val() === “”) {
$(this).val($(this).attr(“title”));
}
});
});
Won’t the blur function change the input value when you move focus to the submit button?
Seems like this probably doesn’t work irl
@Madmartigan No, as it’s only doing it on the inputs with a class of ‘clearMeFocus’
An issue with this code is encountered if you have multiple inputs, the value of clearMePrevious gets placed into the wrong fields.
JavaScript has a nifty attribute that people tend to forget called .defaultValue which stores the default value of a field no matter what you change it to.
I’ve modified your code as follows and it works wonderfully.
// clear input on focus
$(‘.clearMeFocus’).focus(function(){
if($(this).val()==this.defaultValue){
$(this).val(”);
}
});
// if field is empty afterward, add text again
$(‘.clearMeFocus’).blur(function(){
if($(this).val()==”){
$(this).val(this.defaultValue);
}
});
That’s a good point. Thank you
Hey Just wanted to say thanks, it worked great!
Is this code compatible with newer versions of jQuery?
There is no demo on this page, so I can’t check which jQuery version you are using with your script. Additionally, this blog entry was also started almost two years ago. I see you are currently using version 1.4.2 on this page, but with no demo, I’m assuming you are using version 1.4.2 with your script.
I have not been able to get this working with version: jquery-1.4.4.min.js
I’ve tried both the original by Nic Boldson and also Patrick’s code to no avail.
Thanks for any input you can offer with my questions. A working website link would allow me to debug it myself and avoid you typing a detailed reply. If you don’t want to send a link to a personal website, then you have my email address in my post here and I’d be grateful if you could send it along so I can debug the issue.
Thanks
Don
Hi Don,
It should work fine with all versions of jQuery as it’s using the standard syntax. Patrick’s version is a lot slicker and I’ve been using that since
Loving Patrick’s solution. Thanks guys
Pingback: jQuery: Löschen von Formularwerten bei Fokus dank Javascript
Good article. You can use preventDefault rather than return false, though.
Add your Comment