0

I am working on a web application, and i want to remove all the <a> that have the word "question". so i write the following script:-

<script>
$( document ).ready(function() {

    $('a.ms-pivotControl-surfacedOpt:contains("question")').hide();
});
</script>

Now i got this behavioure:-

  1. when the page first loads, the script will be able to hide all the content correctly.

  2. but if i click on a button that will refresh the page in an Ajax-Based approach, then the content that have "Questions" inside it will be shown (the script will not have any effect).

so i think the problem is that the script will not be able to understand the newly added content. i remember on old jquery versions i use to define .Live() to keep my script live and to understand newly added content ,, so is this possible inside my above script ?

John John
  • 1
  • 72
  • 238
  • 501

2 Answers2

1

on() and the deprecated live() are only for event delegation, not for DOM manipulation or plugin initializing.

When you need to manipulate newly inserted html , or initialize a plugin on it, there is no way to delegate that and you need to use the callback of ajax to do it.

Example using simplest ajax method load()

$('#container').load('somefile.html',function(){
   // new html exists, "this" is initial selector element
   $(this).find('.someClass').hide();    
});
charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • but in my case i can not access the ajax call back as this is a third part web application ,, so i can not access the ajax call back in any way... so is there any other approach that i can follow? – John John Aug 11 '15 at 14:42
  • @johnG, that third party web application must be included in your source somehow. If its cdn, then get the code, change it, and include it in your application – AmmarCSE Aug 11 '15 at 14:44
  • @AmmarCSE that's not always the case such as a simple script tag that loads another script – charlietfl Aug 11 '15 at 14:44
  • @AmmarCSE this is a sharepoint web application,, so not sure if this is exposed to me ... – John John Aug 11 '15 at 14:46
  • @johnG worst case can set up an interval timer to check for those elements and then clearInterval. Need more details on how 3rd party behaves – charlietfl Aug 11 '15 at 14:47
  • @charlietfl, ah, good point. Maybe this would help? http://stackoverflow.com/questions/3219758/detect-changes-in-the-dom – AmmarCSE Aug 11 '15 at 14:47
1

In your success handler of ajax add this line after your processing :

 $('a.ms-pivotControl-surfacedOpt:contains("question")').hide();
Abdul Rehman Sayed
  • 6,532
  • 7
  • 45
  • 74
  • i do not own and i can not access the ajax call back as this is a third party solution. i can only write script against what is already provided... – John John Aug 11 '15 at 14:43
  • how about using this : www.api.jquery.com/jQuery.ajaxSetup/ a little raw though as it will subscribe to all ajax requests of your page.. – Abdul Rehman Sayed Aug 11 '15 at 14:52