0

I'd like to use the dotdotdot plugin - now, what i wouldn't like is to create all the links individually: $("#wrapper").dotdotdot() . i'd rather like to be able and listen for divs with the class 'textoverflow_dotdotdot' and then add the link automatically.

something like this:

$(document).on('DIV WAS ADDED','.textoverflow_dotdotdot', function(){
     $(this).dotdotdot();
}

any ideas?

INFO: i'm using jquery 1.7.2

Taha Paksu
  • 15,371
  • 2
  • 44
  • 78
Ben Greene
  • 147
  • 1
  • 15
  • jQuery 'live' for the resuce... It's the perfect case you use it. Check out: http://api.jquery.com/live/ and here you have a code sample for your question: http://stackoverflow.com/questions/1525664/jquery-how-to-bind-onclick-event-to-dynamically-added-html-element – Ido Green Jun 10 '12 at 08:46
  • 3
    @IdoGreen: I'd check out the depreciation warning on `.live()`. – Blender Jun 10 '12 at 08:47
  • @IdoGreen live was replaced by on in my version of jquery - i already had that figured out. now my problem is the event, the user doesn't click or do anything - my page uses a lot of ajax loads and i wouldn't want to call the dotdotdot() individually every time a page was loaded - if there was a smart way to listen globally for the creation of a div with my identifier class:'textoverflow_dotdotdot' i'd really appreciate that – Ben Greene Jun 10 '12 at 08:51
  • Only one I can think of is using DOMNodeInserted, but that's highly discouraged :S http://jsfiddle.net/ZPEbW/ – Joseph Marikle Jun 10 '12 at 09:00
  • Why dont you use `.dotdotdot()` on ajax success ? – Jashwant Jun 10 '12 at 09:11
  • @JosephMarikle i know this one, but it doesn't fire, when i load content dynamically that has my class somewhere down the tree as its offspring – Ben Greene Jun 10 '12 at 09:15
  • @Jashwant that's a good idea! but still it's not the perfect solution, right? lets think of the users with their old pcs, when i'm having a lot of ajax loads and i'm calling the function even though there is no element on the page that needs it - also i'm having an ajax load every 3 seconds to update my notification box, already truncated text then would get the .dotdotdot over and over - wouldn't that be a waste of cpu? – Ben Greene Jun 10 '12 at 09:18
  • Just add it to the elements that need it :). e.g. `$('.dotters').dotdotdot()` and on ajax success, do `addClass('dotters') and then use the `update` event of `dotdotdot` – Jashwant Jun 10 '12 at 09:19
  • @Jashwant sorry, i just edited my comment: also i'm having an ajax load every 3 seconds to update my notification box, already truncated text then would get the .dotdotdot over and over... – Ben Greene Jun 10 '12 at 09:20
  • Just use the `update` event. I can clear further things, if you can share the code (or a part of it) on [jsfiddle](http://jsfiddle.net) – Jashwant Jun 10 '12 at 09:22
  • @Jashwant i didn't think of that, i'll do it this way – Ben Greene Jun 10 '12 at 09:28
  • 1
    @IdoGreen, as `Blender` stated, `live()` has been deprecated as of jQuery 1.7. Also, for anyone using jQuery 1.6 and earlier use `delegate()` instead of `live()`. I believe the reason is mainly that `live()` exhibited memory issues. If you happen to use version 1.3 and earlier use `bind()`. bind() was added in 1.0, `live()` in 1.3, `delegate()` in 1.4.2 and `on()` in 1.7. – Nope Jun 10 '12 at 09:38

1 Answers1

2
$(document).bind('ajaxSuccess', function(){
        $('.textoverflow_dotdotdot').removeClass('textoverflow_dotdotdot').addClass('dotters').change();

})

$(document).on('change','.dotters',function(){
           $(this).dotdotdot();
});

credits go to Jashwant ;-)

Ben Greene
  • 147
  • 1
  • 15