1

I have a div container and a button that when clicked, simply adds a class to an element in my mark-up allowing it to expand into a full screen div.

Before: (start of mark-up)

<section class="isi" data-trigger="toggle" data-trigger-class="isi--show-isi"> 

After class is attached:

<section class="isi isi--show-isi" data-trigger="toggle" data-trigger-class="isi--show-isi"> 

The Problem:

The problem is, I am simply trying to create a new instance of this anchor link to trigger this same attach class to show this div the same way with a different link.

attempt:

$('a.isilnk').on('click', function(){
    $('.isi').addClass('isi--show-isi');
}); 

I have tried writing click function the other way, I have tried toggleClass and I have tried trigger click. But it seems to ONLY attach the class for a brief second and then quickly unattach during this instance any thoughts?

Dr Upvote
  • 8,023
  • 24
  • 91
  • 204
  • 1
    There is one thing I can't get. Where do you try to create a new instance of this button and why? – Christos Dec 23 '15 at 18:20
  • your click event is for anchor not for button. what is button in your html – Farhan Dec 23 '15 at 18:24
  • Any button class you use the same class name on, will be acted upon by jQuery. To have access to the methods or properties of multiple buttons with the same classname, use an array and target each instance by the index number. – Korgrue Dec 23 '15 at 18:26
  • is this wrapped in $(document).ready(function(){.......}); as this seems to work for me. also can you please provide html for button/link too? – Farhan Dec 23 '15 at 18:31
  • Try: $(document).on('click', 'a.isilnk', function( – Wesley Smith Dec 23 '15 at 18:36
  • Possible duplicate of [Event binding on dynamically created elements?](http://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements) – Wesley Smith Dec 23 '15 at 18:44

1 Answers1

1

You need to use the .on() event to access dynamically generated elements like this:

$(document).on('click', 'a.isilnk', function(){
    $('.isi').addClass('isi--show-isi');
}); 
Dr Upvote
  • 8,023
  • 24
  • 91
  • 204