tags:

views:

38

answers:

2

Hi. I've a page on html. When i click on a submit button i call a Javascript function. I need (for example) go at the top of the page (or on another specified place, like in the middle where i have some div). I think there's a method for do it. How is called? Thanks

CODE :

<form onSubmit="return checkTL();" method='POST' action='./index.php?status=add' name='addtl' >
    somethings...
    <input type="submit" name="mgmttlt" value="Add Tracklist" />
</form>

function checkTL() {
    value=$('#inputa').val().replace( /[\\\s]+/g, '' );
    if(value=="") {
        $('#printe').removeClass().addClass('error').html("Please insert a valid Artist");

        var pos = $('.target').offset().top;
        $('html,body').animate({scrollTop: pos },1000);            

        return false;
    }

    return false;
}   

as example, i return always false in the function. But if i add the scrollTop function, in some mode it return true and the function go on. Why?

+2  A: 

Use jQuery's ScrollTo Plugin. Example.

Teja Kantamneni
I second this. I use this with jQuery's accodion component, so that when a user clicks an accordion header, the page scrolls so the header is at the top of the page.
Dutchie432
+2  A: 

This can be done with out a plugin

Refer :.scrollTop()

Example :

$(function() {
  $('.button').click(function() {
    var pos = $('.target').offset().top;
    $('html,body').animate({scrollTop: pos },1000)
  });
});

You can test it here http://jsbin.com/ixabe3

Ninja Dude
@Avinash Yes it can be done like this. Extracting that part of the code in to a reusable code makes it a plugin tough.
Teja Kantamneni
uhm. I don't know why, but it doesnt work :( i posted the code above check it out
markzzz
in the main topic!
markzzz
oh shit...i need to change .target too! hahahah
markzzz
now it rocks!!! thanks !
markzzz
take care, dude =)
Ninja Dude