Clear multiple sections using jquery

Quick question. If I try to clear multiple divs with the same class, is there still a short jQuery for this? I tried $('.clearit').empty(); and doesn't seem to work at all (none of the sections with this class are cleared). Can I do it wrong or is it just not working? Let me know, I tried $('.clearit').html(''); . I also have this along with other code that runs after it in jquery, and everything else works as it should. I debugged the code and seemed to run the function without errors, but the contents of the div were not cleared. Here is an example of divs:

 <center><div class="clearit" id="container2">Loading the player ...</div></center> </li> <li> <center><div class="clearit" id="container3">Loading the player ...</div></center> </li> <li> <center><div class="clearit" id="container4">Loading the player ...</div></center> 

and here is the code for one of the handlers. Please pay attention to everything that works there, except for $('.clearit').html(''); .

 $('.thumbNav li a').click(function(){ $('.clearit').html(''); var str = $(this).html(); var pattern = /[0-9]+/g; var matches = str.match(pattern); var myInteger = parseInt(matches); //variable for player var container = "container"+myInteger; var what = 'skill'+myInteger; var location = "https://aliahealthcare.com/videofetcher.php?id=skill"+myInteger; player(container,what,location); $('#'+what).attr('selected', 'selected'); }); 

THANKS TO THOSE FOR THOSE WHO HELP ME! I also changed the site so that you no longer need to log in so that you can see it in action for yourself. Please use the above code as a reference when searching for javascript. (Everything is in vid.php) Thanks

https://aliahealthcare.com/vid.php

Also, I just discovered something interesting. The upper part of li is part of ul with the slider id. When I do $('#slider').html(''); or $('#slider').empty(); It works, but it clears all divs, so players who load inside these divs do not load. All I have to do is just clear the players so that you cannot work with multiple players at the same time.

+4
source share
3 answers

Please see demo here: http://jsfiddle.net/TxR8K/

updated from your code: http://jsfiddle.net/TxR8K/1/

By the way, in your example there is no tag of an unordered list ie ul :)

Update after after discussion with OP - we also found out - the player is not really part of the html in the div, so he / she knows what needs to be done and clears his./ her path for further correction :)

That should do the trick

  $('.clearit').html(''); 
+3
source

Your problem is probably due to the fact that you execute this command before loading the DOM. Open a console window in StackOverflow and run $("div").empty(); . Everything disappears!

+3
source

Your code should work correctly.

Please check that

  • you put the code in $(document).ready(function() {...}) , in short $(function() {..})

  • you are loading jQuery library correctly

  • do not have typos errors

If everything is ok, then your code should work.

If not, try @Tats_innit answer.

According to you edit

However, I think your code should work.

but if not, you can try the following:

 $('div[id^=container].clearit').empty(); 

Demo

+2
source

All Articles