0

I'm having a hard time on this one, and I cannot find any relevant questions (although I think I have used quite a lot of relevant searchterms (ref: the title)... So here goes:

<td class="noprint">
 <a class="ui-button isbillable" onclick="switchBillable($(this))" value="1">Aanrekenbaar?</a>
 <a class="ui-button isbillable" onclick="switchBillable($(this))" value="2">Aanrekenbaar?</a>
 <a class="ui-button isbillable" onclick="switchBillable($(this))" value="...">Aanrekenbaar?</a>
</td>

I want to execute an AJAX-request (PHP) upon pageload giving the ui-buttons a specific color depending if the AJAX-request returns TRUE or FALSE. This code however is never executed (tested with an 'alert()'):

$(document).ready(function(){
    $( ".isbillable" ).load( function() {
        var prestId = $(this).val();
        $.ajax({
            url: "ajax/check_billable.php",
            data: { q: prestId },
            success: function () {
                if (response == true) {
                    $(this).removeClass('isbillable');
                    $(this).addClass('billable');
                }else{
                    $(this).removeClass('isbillable');
                    $(this).addClass('notbillable');
                }
            }
        });
    }).button({
        icons: { primary: "ui-icon-help" },
        text: false
    });
    $( ".billable" ).button({
        icons: { primary: "ui-icon-notice" },
        text: false
    });
    $( ".notbillable" ).button({
        icons: { primary: "ui-icon-check" },
        text: false
    });
});

Using Firebug I cannot detect any AJAX calls, so this code is never executed - I have tried with load(), on('load',,), bind()

2
  • It's not apparantly clear what you're trying to do? An anchor has no load event, there is nothing to load? If you're trying to wait for jQuery UI or some other plugin, you'll have to tap in to those plugins directly, assuming they have an event that is fired when finished. Commented Dec 26, 2013 at 18:42
  • The (apparently) obvious mistake was indeed that the anchor-element does not have a 'load-event'. I read somewhere that it was a 'limitation' of the DOM, but I thought jQuery had found a way around this limitation and paid no further attention to it. Commented Dec 26, 2013 at 22:29

2 Answers 2

2

There's no load() event on <a> elements. If you want something to happen when the page is loaded, just put it at the toplevel of your document.ready handler.

You also need to use the context: option to make $(this) available in the callback.

$(document).ready(function(){
    $(".isbillable").each(function() {
        var prestId = $(this).val();
        $.ajax({
            url: "ajax/check_billable.php",
            data: { q: prestId },
            context: this,
            success: function () {
                if (response == true) {
                    $(this).removeClass('isbillable').addClass('billable');
                }else{
                    $(this).removeClass('isbillable').addClass('notbillable');
                }
            }
        });
    }).button({
        icons: { primary: "ui-icon-help" },
        text: false
    });
    $( ".billable" ).button({
        icons: { primary: "ui-icon-notice" },
        text: false
    });
    $( ".notbillable" ).button({
        icons: { primary: "ui-icon-check" },
        text: false
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thx. This solved my problem, and Kudos for seeing the other problem that indeed $(this) was not referencing the element I wanted it to.
0

try with

$(window).load(function(){
    $( ".isbillable" ).load( function() {
        var prestId = $(this).val();
        $.ajax({
            url: "ajax/check_billable.php",
            data: { q: prestId },
            success: function () {
                if (response == true) {
                    $(this).removeClass('isbillable');
                    $(this).addClass('billable');
                }else{
                    $(this).removeClass('isbillable');
                    $(this).addClass('notbillable');
                }
            }
        });
    }).button({
        icons: { primary: "ui-icon-help" },
        text: false
    });
    $( ".billable" ).button({
        icons: { primary: "ui-icon-notice" },
        text: false
    });
    $( ".notbillable" ).button({
        icons: { primary: "ui-icon-check" },
        text: false
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.