views:

102

answers:

3

Initially i was looking for an answer to a show/hide page jumping issue. Having found an answer here: http://stackoverflow.com/questions/2024342/link-with-href-scrolls-page-to-top-when-used-with-jquery-slidetoggle, I need to understand where to put "return false" in the following code:

toggleDetail : function(obj) {
    $(obj).parent().parent().next().toggleClass('hide');
    $(obj).text() == 'Show' ? $(obj).text('Hide') : $(obj).text('Show');
},

and here's my call to the show/hide. The 'href="javascript:void(0);"' worked in stopping the page jumping, do i still need the 'return false'?

<a href="javascript:void(0);" onclick="VSASearch.toggleDetail(this)">Show</a>

i tried adding 'return false' to the end of each $(obj) line before the semi-colon, but that was not it.

Thank you for your assistance.

+1  A: 

you want to return false from the onclick hander

<a onclick="VSASearch.toggleDetail(this); return false">Show</a>

Better yet, use unobtrusive javascript and assign event handler in the $() function:

<a id="show">Show</a>

....

$(function() {
 ....
 $("#show").click = function() {
     VSASearch.toggleDetail(this);
     return false;
 }
stereofrog
I don't think an anchor tag without an href will validate will it? Just need to stick in a #.
GenericTypeTea
@GenericTypeTea, how come? href is optional
stereofrog
@Stereo - I don't know for sure. I just assumed you always needed an href, I was asking you the question =D
GenericTypeTea
Yep, you're right, href is optional! http://www.w3schools.com/tags/tag_a.asp. You learn something new every day.
GenericTypeTea
+5  A: 

You just need to return false on the onclick handler. If onclick returns false, then the postback/page reload will be stopped.

<a href="#" onclick="VSASearch.toggleDetail(this);return false;" />

Or you can return the result of your functions as follows:

toggleDetail : function(obj) {
    $(obj).parent().parent().next().toggleClass('hide');
    $(obj).text() == 'Show' ? $(obj).text('Hide') : $(obj).text('Show');
    return false;
},

with

<a href="#" onclick="return VSASearch.toggleDetail(this);" />
GenericTypeTea
A: 
toggleDetail : function(obj) {
    $(obj).parent().parent().next().toggleClass('hide');
    $(obj).text() == 'Show' ? $(obj).text('Hide') : $(obj).text('Show');
    return false;
},
Glennular
Amazing.... the answers are blazing fast. StackOverflow is such a wealth of excellent information and a community so giving of help.Adding "return false" on it's own line works perfectly.Thank you Glennular and GenericTypeTea. I like the idea of having as much as i can in the script file rather than on the page so i'll reside there.thank you.
caroline
@Caroline, that's OK, just don't tell our bosses.
GenericTypeTea