Search code examples
javascriptjquerytipsy

Variable is not defined


I am trying this code to update the fallback of tipsy plugin. How can I access the variable a outside of first function ? I can override the variable to make the update, correct ?

<script type="text/javascript">
$(document).ready(function () {
    var a ="Login";
    $("#login_form").submit(function () {
        var formdata = $("#login_form").serializeArray();
        $.ajax({
            url: "ajax_login.php",
            type: "post",
            dataType: "json",
            data: formdata,
            success: function (data) {
                if (data.livre === 'complete') {
                    var a ="success";
                } else 
                    var a = "Error";
            }
        });
return false;
    });
});
</script>

<script type='text/javascript'>
$(document).ready(function () {
    $('.login_fields input[rel=tipsy]').tipsy({gravity: 'w', trigger: 'manual', fallback: a }); // a is not defined
});
</script>

Solution

  • It is not possible to change the fallback property of your Tipsy objects after they've been created without hacking the tipsy plugin itself.

    Given the fallback parameter is a string, it is interpreted as soon as the $().tipsy({...}) function is executed. So changing the value of variable a afterwards will not change the fallback parameter.

    I first thought that directly updating the fallback property from the $.fn.tipsy.defaults object would make it, but when a new Tipsy object is created, the fallback property is basically copied in it, so it will store the initial value of fallback forever.

    One solution would be to fork the Tipsy project and change the fallback property to accept either a string or a function(). Like this it would be possible to do something like: fallback: function (){ return a;}.