Code needs to run after page is loaded

Below is a snippet of code that doesn't run when I load a page.  Only "-1-" is printed to the console.  It appears that the function inside is not loaded by the time the entire page has loaded.  However, in the firebug console after the page has loaded, I can manually run a commands such as:

$(".listings_display").find("a").removeAttr("onclick");
$(".listings_display").find("a").removeAttr("href");
$(".listings_display").find("a").css('cursor','not-allowed');

and it will remove onlick, href, and change the cursor.

I tried the delegate function and still the commands do not run when page is loaded.  It should do this when the user is not an admin so that the links are no longer active.

The 2nd part of this question is if there is a cleaner way for disabling, removing, hiding links instead of those commands I listed?  The block of code contains <br> for separating and I don't necessarily want to disable all links.  I want to display by a certain condition (such as if the string "ABC", "DEF", etc.  is 3 characters, then disable link.  But if it's 4 characters like "GHIJ" then do not disable the link.  I put in the line below with $(".listings_display").find("a").each(function(){ ...} but at this moment, I need to make sure this function runs when after the page is loaded.

if(!isadmin){
  console.log("-1-");
  //$("body").delegate(".listings_display", "load", function(){
  $(".listings_display").bind("load.listings", function(){
    console.log("-2-");
    $(".listings_display").find("a").removeAttr("onclick");
    $(".listings_display").find("a").removeAttr("href");
    $(".listings_display").find("a").css('cursor','not-allowed');
    //$(".listings_display").find("a").each(function(){
      //console.log("-3-");
    //});
  }).triggerHandler("load.listings");
}

Open in new window

<div class="row">
  <div class="grid2 first">
    <label for="listings">Listings</label>
  </div>
  <div class="grid8">
    <div class="listings_container">
      <input type="hidden" class="listings" id="listings" title="Listings" value="ABC DEF GHIJ KLM" tabindex="-1">
      <div class="listings_display">
ABC <a href="#" onclick="listingsManager.del('ABC', this); return false;" style="display: inline;">[Delete]</a> <a href="#" onclick="listingsManager.undo('ABC', this); return false;" style="display: inline;">[Undo]</a><br>
DEF <a href="#" onclick="listingsManager.del('DEF', this); return false;" style="display: inline;">[Delete]</a> <a href="#" onclick="listingsManager.undo('DEF', this); return false;" style="display: inline;">[Undo]</a><br>
GHIJ <a href="#" onclick="listingsManager.del('GHIJ', this); return false;" style="display: inline;">[Delete]</a> <a href="#" onclick="listingsManager.undo('GHIJ', this); return false;" style="display: inline;">[Undo]</a><br>
KLM <a href="#" onclick="listingsManager.del('KLM', this); return false;" style="display: inline;">[Remove]</a> <a href="#" onclick="listingsManager.undo('KLM', this); return false;" style="display: inline;">[Undo]</a><br>
      </div>
    </div>
  </div>
</div>

Open in new window

mock5cAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobOwner (Aidellio)Commented:
you need to wrap your code in the jQuery init function:

$(function() {
if(!isadmin){
  console.log("-1-");
  //$("body").delegate(".listings_display", "load", function(){
  $(".listings_display").bind("load.listings", function(){
    console.log("-2-");
    $(".listings_display").find("a").removeAttr("onclick");
    $(".listings_display").find("a").removeAttr("href");
    $(".listings_display").find("a").css('cursor','not-allowed');
    //$(".listings_display").find("a").each(function(){
      //console.log("-3-");
    //});
  }).triggerHandler("load.listings");
}
});

Open in new window

0
mock5cAuthor Commented:
The code was already inside an init function $(function() { ... });
0
RobOwner (Aidellio)Commented:
this should do it

$(function() {
if(!isadmin){
  console.log("-1-");
    $(".listings_display").find("a").removeAttr("onclick");
    $(".listings_display").find("a").removeAttr("href");
    $(".listings_display").find("a").css('cursor','not-allowed');
}
});

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.