0

I'm using the selectText function from here https://stackoverflow.com/a/987376/784637

However when I called this function immediately after an ajax request on the newly created element:

selectText('some-newly-added-element')

I get the following error in firebug

NS_ERROR_DOM_NOT_OBJECT_ERR: Parameter is not an object
[Break On This Error]   

range.selectNodeContents(text);

Note that I am able to call this function after the ajax request on the same element like so

$('#container').on('click', '#some-newly-added-element', function(){
   selectText('some-newly-added-element');            
});

Is there a way to call this function immediately after the ajax request is done?

Community
  • 1
  • 1
user784637
  • 15,392
  • 32
  • 93
  • 156

2 Answers2

1

Re: Is there a way to call this function immediately after the ajax request is done?

Do you mean after success of the ajax success. or http://api.jquery.com/jQuery.ajax/

or This should help: Execute function after all ajax .load() requests are finished

Hope this fits the cause :)

Sample

$.ajax({
    url: this.html_url,
    cache: false,
    success: function(html){
        doSomething();
        return true;
    }
});
Community
  • 1
  • 1
Tats_innit
  • 33,991
  • 10
  • 71
  • 77
  • I tried this, so using your example I do `result.html(html); selectText('some-newly-added-element'); ` and it still doesn't work. note that `#some-newly-added-element` is part of the `html` response – user784637 Oct 13 '12 at 09:19
  • Hmmm @user784637 take a look in here man: http://stackoverflow.com/questions/2502828/jquery-execute-function-after-all-ajax-load-requests-are-finished ; lemme know how it goes! `:)~ – Tats_innit Oct 13 '12 at 09:21
1

use complete callback which will trigger after ajax call

  $.ajax({

  complete: function(){
       selectText('some-newly-added-element');
      }

   });

Documentation for ajax event: http://docs.jquery.com/Ajax_Events

YogeshWaran
  • 2,291
  • 4
  • 24
  • 32