javascript - How do invoke an already existing jquery function on an html anchor? -
i created expandable content using jquery on several divs unique ids. code working want trigger function top navigation using anchor. have tried several things nothing works. new jquery appreciated.
i trying invoke click()
function on each anchor.
here jquery:
(function($) { $(document).ready(function () { var panelspeed = 500; var totalpanels = 6; var defaultopenpanel = 0; var accordian = true; var panelheight = new array(); var currentpanel = defaultopenpanel; var iconheight = parseint($('.icon-close-open').css('height')); var highlightopen = true; //initialise collapsible panels function panelinit() { (var i=1; i<=totalpanels; i++) { panelheight[i] = parseint($('#cp-'+i).find('.expandable-panel-content').css('height')); $('#cp-'+i).find('.expandable-panel-content').css('margin-top', -panelheight[i]); if (defaultopenpanel == i) { $('#cp-'+i).find('.icon-close-open').css('background-position', '0px -'+iconheight+'px'); $('#cp-'+i).find('.expandable-panel-content').css('margin-top', 0); } } } $('.expandable-panel-heading').click(function() { var obj = $(this).next(); var objid = parseint($(this).parent().attr('id').substr(3,2)); currentpanel = objid; if (accordian == true) { resetpanels(); } if (parseint(obj.css('margin-top')) <= (panelheight[objid]*-1)) { obj.clearqueue(); obj.stop(); obj.prev().find('.icon-close-open').css('background-position', '0px -'+iconheight+'px'); obj.animate({'margin-top':0}, panelspeed); if (highlightopen == true) { $('#cp-'+currentpanel + ' .expandable-panel-heading').addclass('header-active'); } } else { obj.clearqueue(); obj.stop(); obj.prev().find('.icon-close-open').css('background-position', '0px 0px'); obj.animate({'margin-top':(panelheight[objid]*-1)}, panelspeed); if (highlightopen == true) { $('#cp-'+currentpanel + ' .expandable-panel-heading').removeclass('header-active'); } } }); function resetpanels() { (var i=1; i<=totalpanels; i++) { if (currentpanel != i) { $('#cp-'+i).find('.icon-close-open').css('background-position', '0px 0px'); $('#cp-'+i).find('.expandable-panel-content').animate({'margin-top':-panelheight[i]}, panelspeed); if (highlightopen == true) { $('#cp-'+i + ' .expandable-panel-heading').removeclass('header-active'); } } } } //uncomment these lines if expandable panels not fixed width , need resize $( window ).resize(function() { panelinit(); }); $(window).load(function() { panelinit(); }); //end load }); //end ready })(jquery);
the html expandable jquery content is:
<div class="expandable-panel" id="cp-3"> <div class="expandable-panel-heading"> <h2>testimonials<span class="icon-close-open"></span></h2> </div> <div class="expandable-panel-content"> <p>panel html...</p> </div> </div>
the html anchors want trigger click()
function are:
<ul class="nav"> <a href="#cp-1" onclick="$( "a" ).trigger( "click" );"><li class="nav">what justice court judge?</li></a> <a href="#cp-2"><li class="nav">about reeves jones</li></a> <a href="#cp-3"><li class="nav">testimonials</li></a> <a href="#cp-4"><li class="nav">polling locations</li></a> <a href="#cp-5"><li class="nav">map</li></a> <a href="#cp-6"><li class="nav">contact</li></a> </ul>
thank in advance help!
if understand correctly, don't want trigger click event anchors, instead want trigger click event of corresponding navigation element when anchor clicked.
you can adding following code inside document-ready function:
$('.nav a').click(function() { $($(this).attr('href')).find('.expandable-panel-heading').click(); });
this code registers click handler on <a>
elements inside .nav
element. uses href
value of clicked <a>
element jquery object representing navigation element.
in case, should not place "onclick" attribute on <a>
elements.
btw: valid html, should nest <a>
elements inside <li>
elements, not other way around.
Comments
Post a Comment