Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
126
rated 0 times [  128] [ 2]  / answers: 1 / hits: 23169  / 12 Years ago, thu, may 31, 2012, 12:00:00

hey i have a jquery function that is triggered on an anchor's onclick event the function is as follows:



function dropDown(subid, e) {
e.preventDefault();
var sub = '#' + subid;
// hide all submenus first
$('.subnav').css({'display' : 'none'});
//Show the Current Subnav
$(sub).css({'display' : 'block'});
}


this is how i am tring to trigger it :



<a href=#! onclick=dropDown('sn_cities') >Cities</a>


However i am getting this error: e is undefined



i want to cancel the default onclick event of the anchor link, any help would be appreciated.


More From » jquery

 Answers
9

You're not passing the event object (or anything at all) to the e parameter of the function. Try this:



onclick=dropDown('sn_cities',event);


I'd be inclined to lose the inline JS altogether though:



<a href=#! data-dropDown=sn_cities>Cities</a>

$(document).ready(function() {

$(a[data-dropDown]).click(function(e) {
e.preventDefault();
// hide all submenus first
$('.subnav').hide();
//Show the Current Subnav
$('#' + $(this).attr(data-dropDown)).show();
});

});

[#85252] Tuesday, May 29, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
juancarlos

Total Points: 580
Total Questions: 105
Total Answers: 103

Location: Grenada
Member since Sun, Dec 20, 2020
3 Years ago
;