views:

57

answers:

5

Hi guys,

Novice question I guess:

The Scenario:
I have an asp.net page that contains 6 divs and 6 hyperlinks which are linked to javascript function:

function showdiv(divname)
{
   hideallcontentdivs();
   var targetdiv = document.getElementById(divname);
   targetdiv.style.display="block";
}

var alldivs=new Array();
function hideallcontentdivs()
{
   alldivs=document.getElementsByTagName("div");
   for(var i=0; i<alldivs.length;i++)
   {
      if(alldivs[i].className=="content")
      {
         alldivs[i].style.display="none";
      }
   }
}

ImageButton:

<a href="" onclick="showdiv('item1');"><img alt="alternate_text" src="imagesource" border="0px" /></a>

the divs have id's: item1, item2....item6 and the hyperlink specify to the javascript function the id of the div that needs to displayed.

The Problem:
Whenever I click on an hyperlink, the effect that I want is achieved but the page reloads too. Don't really know where I'm going wrong but if anyone could guide me in the right direction, I'll be very very thankful.

Thanks

+5  A: 

Just return false on click

onclick="showdiv('item1');return false;"

Update:I just point the issue :), you can return the false where ever you like.

onclick="return showdiv('item1');"

and say to showdiv() to return false

Aristos
Thanks a lot sir! That worked like a charm, as I mentioned, it was indeed a novice question as Javascript to me is still like Chinese, just starting to learn it. Thanks once again.
Anchit
can you not just do onclick="showdiv('item1');" without the 'return' statement when you have 'return false' in the showdiv method? saves a word and same result.
Stefanvds
As I wrote in my answer, I would strongly recommend to have a look at [jQuery](http://www.jquery.com). It's bad style to write behaviour directly into the html's onSomeEvent attributes.
Dave
@Dave I agree with you for the jQuery, I just point out the return. @Stefanvds Yes its work the same, I just use the return in my code style (when I do not use jQuery) for remind me that I need to return something and be 100% sure that is return it. Also some times the return can be true or false depend from some criteria inside the function.
Aristos
+3  A: 

always, return false :)

but i'd rather put it in the function

function showdiv(divname)
{
   hideallcontentdivs();
   var targetdiv = document.getElementById(divname);
   targetdiv.style.display="block";
   return false;
}
Stefanvds
+1  A: 

+1 for Aristos' answer, although if you refactor the return false; to the end of the showdiv function you want have to update all the links.

RogerNoble
+4  A: 

You should add return false to your onclick handler. This prevents the default behaviour of the link.

A better solution would be to use jQuery or some other library and attach the event to the element. They also contain special functions to prevent the default behaviour:

Html:

<a id="button1" href="#"><img alt="alternate_text" src="imagesource" /></a>

Javascript/jQuery:

$('#button1').click(function(event) {
    $('div').hide();        // hides all divs
    $('#div1').show();      // shows the div with the id 'div1'
    event.preventDefault(); // prevents postback
});
Dave
nice info, but this guy is just starting with javascript. i think it's a bit far fetched for him right now :)
Stefanvds
Yeah, he is just starting, and I think it's better to learn it the right way from the beginning ;-)
Dave
Stefan, yes it is still a bit far-fetched for me.Dave, I dont want to hide all the divs on the page, only the ones having their css class as 'content'.
Anchit
One question: Javascript can be disabled from the browser, is it the same for jQuery? I guess not, and if it can't be then I guess it is a better bet than Javascript. I must say I've gotta lot to catch up. o_O
Anchit
To only hide the divs with the class 'content' you can change the second line to `$('.content').hide()`. jQuery is a JavaScript library and if JavaScript is disabled, then jQuery wouldn't work either. The advantage is, that you can build a Site that works without JavaScript and then inject the client side behaviour via jQuery and improve the site. You could for example change your code `<a id="button1" href="#div1">...</a>` which would cause the page scroll to the appropriate div, when JavaScript is disabled.
Dave
Here's an example: http://jsfiddle.net/k2RQc/ (you can delete the Javascript for testing the behaviour with Javascript disabled)
Dave
+1  A: 

return false is the key, as the other answers have said.

Just to add my tuppence though: you don't really need to use an anchor here and imho it's not good semantic style. Just use a span or div instead, and then the problem goes away.

fearofawhackplanet