Jump to content

Not sure where to start looking/preventDefault() not preventing.


delayedinsanity

Recommended Posts

jQuery(document).ready(function($) {
$("a#help-button").click(function(e) {
	var spanid = $($(this).attr("href"));
	spanid.slideToggle('slow');
	e.preventDefault();
	return false;
 });
});

 

I'm sure there's probably a better way of doing what I'm doing here, so if you have any tips for that I'm all ears.

 

The problem though is in preventDefault/return false... neither is working (on their own or together as illustrated) to stop the browser from trying to find the named anchor from the A element. So if I have <a href="#keyword" id="help-button">...</a> it appends #keyword to the url... I just want to use the href to find the element it should open, I don't want it to scroll anywhere.

 

Link to comment
Share on other sites

Yeah no I tried that... due to my lack of experience with jQuery/JavaScript I also tried function(e) { e.preventDefault(); and function(event) { event.prevent... as I've seen it said both ways (by people who claim their way is the only way, but thats besides the point).

 

I have a feeling something else is conflicting with it, because I've successfully done this before, but I have no idea what could do that, or how to track it down. I'm an avid Firebug fan but it doesn't show anything in the console about regular calls like this, and I don't know if I can figure out the DOM page.

 

Link to comment
Share on other sites

Only problem with that is that I'm using the value of the href to determine what element the toggle should occur on.

 

I think what's wierd is as I mentioned, I've used both of these methods before with absolutely no problem (return false or .preventDefault), it's just in this one case that it's being a buzzard. The other option I'm considering is removing the A element altogether and just making a clickable div.. then I can use the ID of the div to attach the toggle to a span of the same class name.

 

Link to comment
Share on other sites

What do you think of this approach:

<a href="javascript: void(0);" id="blah" rel="toggleDiv">...</a>

 

JavaScript:

jQuery(document).ready( function() {
     jQuery("#blah").onclick( function() {
          jQuery("#" + $(this).attr("rel")).slideToggle("slow");
          return false;
     });
});

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.