Search code examples
jquerydynamichrefpanelbind

How to change jQuery panel href?


I have an record with an href to a panel. In differing states, I would like the button to open different panels. I've tried changing the href, but that doesn't appear to be enough, as the button always opens the original panel.

The a record is defined like this ...

  <li><a href="#panel1" id="Xpanel" class="icon fa-crop">&nbsp;</a>

When clicked, panel1 is displayed and works great ...

I'm changing the href for the a record like so ...

   var id = document.getElementById('Xpanel');
   $(id).attr('href', "#panel2");

In the Safari debugger, I can see that all is changing, but the button always opens the original #panel1.

Do I need to rebind the records href somehow ???


Solution

  • It sounds like you are creating an event listener on Xpanel with a locally scoped variable, i.e. Xpanel's href at the time. If you update the href the event listener doesn't use the updated value when it fires; it uses the original value when the listener was created.